[英]How to invoke method, using reflection, on instantiated object?
I have some base objects "Car", "dog", "cat" they implement an interface "IGWUIElement". 我有一些基础对象“ Car”,“ dog”,“ cat”,它们实现了接口“ IGWUIElement”。 I have a list of these interfaces: List myList.
我有这些接口的列表:列出myList。
At runtime I loop through the list of my elements and by examining the name of the class (using reflection) i need to populate their properties - which are not a part of the interface). 在运行时,我遍历元素列表,并检查类的名称(使用反射),我需要填充其属性-这不是接口的一部分)。 I have an xml document descripting the propeties and the value i should assign to them.
我有一个xml文档,描述了属性和应分配给它们的值。 Here is my interface instantiation.
这是我的界面实例化。
IGWUIElement newUIElement = (IGWUIElement)Activator.CreateInstance(result);
How do I go about invoking the properties from their name with a particular value (Please note datatypes are limited to int and string). 如何从属性名称中使用特定值调用属性(请注意,数据类型仅限于int和string)。 Each object have different properties.
每个对象都有不同的属性。
Hope this makes sense... 希望这有道理...
/H4mm3r / H4mm3r
You can do this like so: 您可以这样做:
IGWUIElement element = myList[0];
// Set a string property
element.GetType().InvokeMember("SomeStringProperty", BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty, Type.DefaultBinder, element, "The String Value");
// Set an int property
element.GetType().InvokeMember("SomeIntProperty", BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty, Type.DefaultBinder, element, 3);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.