Reputation: 2237
See below code and can someone help me....
public class person
{
Public string name { get; set; };
Public personDetails Pdetails { get; };
}
public class personDetails
{
Public bool hasChild { get; set; }
Public string ChildName { get; set; }
}
static void Main(string[] args)
{
Type type = asm.GetType(person);
object classInstance = Activator.CreateInstance(type);
PropertyInfo prop = type.GetProperty("Pdetails ", BindingFlags.Public | BindingFlags.Instance);
if (null != prop && prop.CanWrite)
{
prop.SetValue(classInstance, null , null);
}
}
getting Error for property not found.
Upvotes: 2
Views: 1849
Reputation: 2237
static object GetPropertyValue(object obj, string propertyName)
{
var objType = obj.GetType();
var prop = objType.GetProperty(propertyName);
return prop.GetValue(obj, null);
}
static void SetPropertyValue(object obj, string propertyName, int values)
{
var objType = obj.GetType();
var prop = objType.GetProperty(propertyName);
prop.SetValue(obj, values, null);
}
Thanks for you support i have use upper code to resolved my problem.
Upvotes: 0
Reputation: 485
Type type = asm.GetType("person");
You can only pass type of the object in string format to the asm.GetType function. Another this is that where did you declare the asm object. if you didnt, then define it first.
Upvotes: 0
Reputation: 52808
The property Pdetails
is not public
, so your BindingFlags
should be
BindingFlags.NonPublic | BindingFlags.Instance
Also, see Joel Etherton's answer.
Upvotes: 3
Reputation: 37543
You have an extra space character in your property name. "Pdetails "
is not the same as "Pdetails"
.
Upvotes: 3
Reputation: 56537
Class members are private
by default. Make your properties public
and it should work. Also, remove that extra space on the property string: "Pdetails"
.
Upvotes: 3