Reflection-[Waiting for 2 days ]
-
:confused: Hi! I am developing a mulititier application. I created some value classes with custom attributes specification.And i have seperate query builder class.This query builder class will construct the query based on the valueobject.Internally the query builder using reflection reads all propertie's name and custome attributes.Upto that there is no problem.If i try to fetch the value of a property then the runtime error will occurs.For fetching the value i am using PropertyInfo.GetValue method. So please any on help me to resolve this problem.. Type type=Obj.GetType(); foreach(PropertyInfo f in type.GetProperties()) { foreach(Attribute attr in Attribute.GetCustomAttributes(f)) { if(attr.GetType()==typeof(DefaultValueAttribute)) { str+=":"+f.Name+":"+((ColumnMappingAttribute)attr).Name; str+=f.GetValue(null,null); --This line shows error } } } Please help me to solove the problem M.Sendilkumar Senior Software Engineer TVS Infotech Chennai,TamilNadu,India. 98413 27002
-
:confused: Hi! I am developing a mulititier application. I created some value classes with custom attributes specification.And i have seperate query builder class.This query builder class will construct the query based on the valueobject.Internally the query builder using reflection reads all propertie's name and custome attributes.Upto that there is no problem.If i try to fetch the value of a property then the runtime error will occurs.For fetching the value i am using PropertyInfo.GetValue method. So please any on help me to resolve this problem.. Type type=Obj.GetType(); foreach(PropertyInfo f in type.GetProperties()) { foreach(Attribute attr in Attribute.GetCustomAttributes(f)) { if(attr.GetType()==typeof(DefaultValueAttribute)) { str+=":"+f.Name+":"+((ColumnMappingAttribute)attr).Name; str+=f.GetValue(null,null); --This line shows error } } } Please help me to solove the problem M.Sendilkumar Senior Software Engineer TVS Infotech Chennai,TamilNadu,India. 98413 27002
If you would have read teh documentation for PropertyInfo.GetValue you would have seen that the first parameter to it is the "The object whose property value will be returned.". If you read the Remarks section there, it says "Because static properties belong to the type, not individual objects, get static properties by passing a null reference (Nothing) as the object argument." But since you're trying to retrieve a non-static property that line should look like this:
str+=f.GetValue(obj,null);
So next time instead of "Waiting for 2 days" try "Reading the documentation for 5 minutes" -
If you would have read teh documentation for PropertyInfo.GetValue you would have seen that the first parameter to it is the "The object whose property value will be returned.". If you read the Remarks section there, it says "Because static properties belong to the type, not individual objects, get static properties by passing a null reference (Nothing) as the object argument." But since you're trying to retrieve a non-static property that line should look like this:
str+=f.GetValue(obj,null);
So next time instead of "Waiting for 2 days" try "Reading the documentation for 5 minutes":)HI Roman Rodov Thanks for direct me to have a correct solutions.I tried it, it is working fine. M.Sendilkumar Senior Software Engineer TVS Infotech Chennai,TamilNadu,India. 98413 27002