Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?
Posted

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

Something like this:

C#
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;
            }
        } 
    }
 
Share this answer
 
Comments
VJ Reddy 6-Jun-12 13:17pm    
Good answer. 5!
Manas Bhardwaj 6-Jun-12 16:05pm    
Thanks!
taha bahraminezhad Jooneghani 6-Jun-12 16:51pm    
very good!5!
Manas Bhardwaj 6-Jun-12 16:56pm    
thanks!
Maciej Los 6-Jun-12 17:47pm    
Good answer, my 5!
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:


C#
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);
    }
}
 
Share this answer
 
Comments
Manas Bhardwaj 6-Jun-12 16:05pm    
Nicely explained +5
VJ Reddy 6-Jun-12 19:36pm    
Thank you, Manas :)
Maciej Los 6-Jun-12 17:47pm    
Interesting, my 5!
VJ Reddy 6-Jun-12 19:37pm    
Thank you, losmac :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900