看过一些关于反射System.Reflection的文章,但大多数都是获取某个类的结构的介绍,我想了解怎么可以直接访问某个类的实例中一个属性 如给该属性赋值调用方法.
我需要写一个框架 根据system.Controls下的所有对象的name去匹配某个类的事例或结构中的所有属性,如果有相同且类型一致 则自动赋值,遍历所有system.Controls下的对象和 遍历类下的所有属性我都知道,但遍历类下的所有属性 出来的不是事例以下的例子是列出类的所有属性列表,
如何才能给类下的实例中的属性赋值.public class TypeMain
{
    public static void Main() 
    {
        Type myType =(typeof(MyTypeClass));
        // Get the public properties.
        PropertyInfo[] myPropertyInfo = myType.GetProperties(BindingFlags.Public|BindingFlags.Instance);
        Console.WriteLine("The mumber of public properties is {0}.", myPropertyInfo.Length);
        // Display the public properties.
        DisplayPropertyInfo(myPropertyInfo);
        // Get the nonpublic properties.
        PropertyInfo[] myPropertyInfo1 = myType.GetProperties(BindingFlags.NonPublic|BindingFlags.Instance);
        Console.WriteLine("The number of protected properties is {0}.", myPropertyInfo1.Length);
        // Display all the nonpublic properties.
        DisplayPropertyInfo(myPropertyInfo1);        
    }
    public static void DisplayPropertyInfo(PropertyInfo[] myPropertyInfo)
    {
        // Display information for all properties.
        for(int i=0;i<myPropertyInfo.Length;i++)
        {
            PropertyInfo myPropInfo = (PropertyInfo)myPropertyInfo[i];
            Console.WriteLine("The property name is {0}.", myPropInfo.Name);
            Console.WriteLine("The property type is {0}.", myPropInfo.PropertyType);
        }
    }
}谁开发过类似的东西指点一下小弟 先谢了

解决方案 »

  1.   

    static void SetControlPropertyValue(
    string controlName, string propertyName, object newValue)
    {
    if (testForm.InvokeRequired)
    {
    Thread.Sleep(delay);
    testForm.Invoke(new SetControlPropertyValueHandler(SetControlPropertyValue), new object[]{controlName, propertyName, newValue});
    return;
    }
    Type t1 = testForm.GetType();
    FieldInfo fi = t1.GetField(controlName, flags);
    object ctrl = fi.GetValue(testForm);
    Type t2 = ctrl.GetType();
    PropertyInfo pi = t2.GetProperty(propertyName, flags);
    pi.SetValue(ctrl, newValue, new object[0]);
    }
    delegate void SetControlPropertyValueHandler(string controlName, string propertyName, object newValue);
      

  2.   

    SetControlPropertyValue("textBox1", "Text", "yellow");
      

  3.   

    使用
    PropertyInfo的SetValue()方法,详细可见msdn
      

  4.   

    SetControlPropertyValue("textBox1", "Text", "yellow");===============================
    我是要用成类似这样
    myclass mcs=new myclass()
    SetClassPropertyValue(mcs,"属性名","新的属性值")
    是不是只要把
    Type t1 = testForm.GetType();
    改成
    Type t1 = myclass.GetType();还有
    =============
    if (testForm.InvokeRequired)
    {
    Thread.Sleep(delay);
    testForm.Invoke(new SetControlPropertyValueHandler(SetControlPropertyValue), new object[]{controlName, propertyName, newValue});
    return;
    }
    ================================
    是什么意思我不是很理解 
    不管怎么样还是谢谢sskset(断点) 和kissknife(侧身向南边)
      

  5.   

    写错 
    我是要用成类似这样
    myclass mcs=new myclass()
    SetClassPropertyValue(mcs,"属性名","新的属性值")
    是不是只要把
    Type t1 = testForm.GetType();
    改成
    Type MyType = Type.GetType("myclass ");//或Type t1 = myclass.GetType();
      

  6.   

    Type type = typeof(mcs);
    PropertyInfo pi = type.GetProperty("属性名");
    pi.SetValue(mcs, "新的属性值");
    mcs就是实例