使用参数获取类的属性 [英] use parameter to get class's property

查看:53
本文介绍了使用参数获取类的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想要一个应该像这样的功能:

Hi,

I would like to have a function which should be something like that:

public string GetClassProperty(string propertyName)
{
   MyClass entity = new MyClass();
   return entity.[propertyName];
}

public MyClass
{
   public string Name { get; set; }
   public string Desc { get; set; }
}



含义:我向函数传递了属性名称,该函数将返回类的相关属性

有任何想法吗?



meaning: I pass the function the property name, and the function will return my class''s related property

Any ideas?

推荐答案

查看MSDN文档 http://msdn.microsoft.com/zh-CN/library/kz0a8sxy.aspx [ ^ ]

像这样的东西:

Check out MSDN documentation http://msdn.microsoft.com/en-us/library/kz0a8sxy.aspx[^]

Something like this:

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(GetClassProperty("A"));
            Console.WriteLine(GetClassProperty("B"));
            Console.ReadLine();
        }

       public static string GetClassProperty(string propertyName)
        {
           MyClass myClass = new MyClass();
           Type type = myClass.GetType();
           return type.GetProperty(propertyName).GetValue(myClass, null).ToString();
        }
    }

    public class MyClass
    {
        public int A
        {
            get
            {
                return 100;
            }
        }

        public int B
        {
            get
            {
                return 101;
            }
        } 
    }


要获取具有语法[ ] Indexer 的类的property ,请参见此处http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx [ http://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo.aspx [
To get the property of a class with the syntax [ ] Indexer explained here http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx[^] can be used.

Since, a property of the class is required to be returned the return type can be PropertyInfo class explained here http://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo.aspx[^]

The GetValue and SetValue methods of PropertyInfo requires the object whose property value is required. In the present case since the property is of the class which returned it, it is redundant to again pass the instance of the class to get the value or set the value of the property.

Hence, a class InstancePropertyInfo is created as shown below:


void Main()
{
    MyClass myclass = new MyClass();
    myclass.Name="MyName";
    InstancePropertyInfo pinfo = myclass["Name"];
    Console.WriteLine (pinfo.GetValue());
    
    myclass["Desc"].SetValue("Description---");
    Console.WriteLine (myclass["Desc"].GetValue());
    
    //Output
    //MyName
    //Description---
}

public class MyClass {
    public InstancePropertyInfo this[string propertyName] {
    	get {
    	return 
    		new InstancePropertyInfo (this, this.GetType().GetProperty(propertyName, 
    		BindingFlags.Instance | BindingFlags.Public));
    	}
    }
    public string Name { get; set; }
    public string Desc { get; set; }
}
public class InstancePropertyInfo {
    PropertyInfo propertyInfo;
    object target;
    
    public InstancePropertyInfo (object target, PropertyInfo propertyInfo) {
    	this.target = target;
    	this.propertyInfo=propertyInfo;
    }
    
    public object GetValue () {
    	return propertyInfo.GetValue(target, null);
    }
    public void SetValue(object value) {
    	propertyInfo.SetValue(target,value,null);
    }
}


这篇关于使用参数获取类的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆