Thursday, March 13, 2014

Resourcees Writer and Management Class in C Sharp




Make a Windows form application in which manage and write resources by using Resources.

ResourceWriter Code:

usingSystem.Resources;
namespacewriting_resources
{
classProgram
    {
staticvoid Main(string[] args)
        {
try
            {
ResourceWriterrw = newResourceWriter("References.resources");
Imageimg;
img = Image.FromFile("Butterfly.jpg");
rw.AddResource("FirstImage", img);
rw.AddResource("FirstName", "First");
rw.AddResource("FirstPhone", "00112233");
img = Image.FromFile("Child.jpg");
rw.AddResource("SecondImage", img);
rw.AddResource("SecondName", "Second");
rw.AddResource("SecondPhone", "23423");
img = Image.FromFile("Bike.jpg");
rw.AddResource("ThirdImage", img);
rw.AddResource("ThirdName", "Third");
rw.AddResource("ThirdPhone", "2134");
img = Image.FromFile("Smile.jpg");
rw.AddResource("FourthImage", img);
rw.AddResource("FourthName", "Fourth");
rw.AddResource("FourthPhone", "4134");
Console.WriteLine("Resources Successfully writed");
rw.Close();
            }
catch (Exception ex)
            {
Console.WriteLine(ex.Message);
            }
        }
    }
}




Resource Manager Code:

usingSystem.Resources;
usingSystem.Reflection;
usingSystem.Collections;

namespaceResourcesManager
{
publicpartialclassForm1 : Form
    {
public Form1()
        {
InitializeComponent();
        }
ResourceManagerrm;
privateAssembly ass;
privateArrayList phones = newArrayList();
privatevoid button1_Click(object sender, EventArgs e)
        {
ass = Assembly.GetExecutingAssembly();
rm = newResourceManager("ResourcesManager.References", ass);
for (int count = 1; count<=4; count++)
            {
this.list_name.Items.Add(rm.GetString(count+"Name"));
this.img_List.Images.Add((Image)rm.GetObject(count+"Image"));

this.phones.Add(rm.GetString(count+"Phone"));
            }
pic_image.Image=this.img_List.Images[0];
this.list_name.SelectedIndex=0;
this.phone_list.Text=(string)this.phones[0];
this.btnnext.Enabled=true;
        }
privatevoidlist_name_SelectedIndexChanged(object sender, EventArgs e)
        {
this.phone_list.Text = (string)this.phones[this.list_name.SelectedIndex];
this.pic_image.Image=this.img_List.Images[this.list_name.SelectedIndex];
        }
privatevoidbtnnext_Click(object sender, EventArgs e)
        {
int index = this.list_name.SelectedIndex;
if (index < 4)
            {
index += 1;
this.list_name.SelectedIndex = index;
            }
else
            {
index+=0;
this.list_name.SelectedIndex=index;
            }}
privatevoid Form1_Load(object sender, EventArgs e){
        }
    }
 


Wednesday, February 19, 2014

Reflections in C# ( A Simple Process To Get Info About an Assembly ) Assemblies Dealings




Make a program by using concept of reflection in which we get the type related to object.


HelpingClass.cs
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace reflectionlibrary
{
enumColorEnum
    {
        red = 1,
        green = 2,
        blue = 3
    }
interfaceIBase
    {
String Method1(string a);
double Calculate(int a, double b);
    }
publicclassClass1 : IBase
    {
String name = "Muhammad Abdullah";
staticint rate = 10;
public Class1()
        {
Console.WriteLine("Class1 Implemented");
        }
publicstring Method1(string a)
        {
Console.WriteLine("Method1 Called");
return name;
        }
publicdouble Calculate(int a, double b)
        {
Console.WriteLine("Calculate Called");
return a * b;
        }
structs
    {
int a;
string b;
publicvoid same()
        {
            a = 11;
            b = "Muhammad Abdullah";
        }  }





Form1.cs:

using System;
using System.Windows.Forms;
using System.Reflection;
namespace reflection_labtask
{
publicpartialclassReflection : Form
    {
Type[] t;
MethodInfo[] m;
ParameterInfo[] p;
ConstructorInfo[] c;
public Reflection()
        {
            InitializeComponent();
        }
privatevoid Form1_Load(object sender, EventArgs e)
        {Assembly as1 = Assembly.LoadFrom("reflectionlibrary.dll");
            t = as1.GetTypes();
foreach (Type t1 in t)
            {type.Items.Add(t1.Name);}
        }
privatevoid type_SelectedIndexChanged(object sender, EventArgs e)
        {int a = type.SelectedIndex;
            m = t[a].GetMethods();
            namespac.Text = t[a].Namespace;
if (t[a].BaseType != null)
            {basee.Text =Convert.ToString( t[a].BaseType);}
Else basee.Text = "No Base type";
            c = t[a].GetConstructors();
foreach (ConstructorInfo ci in c)
            {constructures.Items.Add(ci);}
            methodd.Items.Clear();
foreach (MethodInfo me in m)
            {
                methodd.Items.Add(me.Name);
            }
        }
privatevoid methodd_SelectedIndexChanged(object sender, EventArgs e)
        {
int o = methodd.SelectedIndex;
            p = m[o].GetParameters();
            parameter.Items.Clear();
            datatype.Items.Clear();
foreach (ParameterInfo par in p)
            {parameter.Items.Add(par.Name);
                datatype.Items.Add(par.ParameterType.Name);
            } }
privatevoid label5_Click(object sender, EventArgs e)
        {} } }