通过反射给对象的对象的属性赋值
请问如何通过反射给Integration类中的Config赋值呢?问题和http://q.cnblogs.com/q/45025/类似
代码如下:
class Test
{
public static void GetInfo(string path, string newValue)
{
//load dll
Assembly asm = Assembly.LoadFrom(path);
Type type = asm.GetType("Integration");
PropertyInfo piConfig = type.GetProperty("Config", BindingFlags.IgnoreCase | BindingFlags.NonPublic | BindingFlags.Static);
PropertyInfo piName = piConfig.PropertyType.GetProperty("Name");
piName.SetValue(??, Convert.ChangeType(newValue, piName.PropertyType), null);
}
}
class Integration
{
internal static Config Config { get; set; }
}
class Config
{
public string Name { get; set; }
public string URL { get; set; }
}
有大神知道如何解决么?
------解决方案--------------------SetValue 不就行了吗
------解决方案-------------------- typeof(Integration).GetProperty("Config", BindingFlags.IgnoreCase
------解决方案-------------------- BindingFlags.NonPublic
------解决方案-------------------- BindingFlags.Static).SetValue(null, new Config() { Name = "a", URL = "b" }, null);
------解决方案--------------------通过spring配置
------解决方案--------------------无解了,Integration是通过emit实例化的,代码无法获取emit中的对象,不知道那位大神能够看得懂
C# code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
DynamicMethod method = new DynamicMethod("DynamicCreate", destinationType, new Type[] { typeof(IDataRecord) }, destinationType, true);
ILGenerator il = method.GetILGenerator();
LocalBuilder result = il.DeclareLocal(destinationType);
il.Emit(OpCodes.Newobj, destinationType.GetConstructor(Type.EmptyTypes));
il.Emit(OpCodes.Stloc, result);
il.Emit(OpCodes.Newobj, type.GetConstructor(Type.EmptyTypes));
for (int i = 0; i < dataRecord.FieldCount; i++)