13,190
社区成员
发帖
与我相关
我的任务
分享
public static object Create(string name)
{
Assembly assembly;
object o = null;
object p;
string rootPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) +
Path.DirectorySeparatorChar;
ConfigInfo cfgInfo = (ConfigInfo)ConfigurationSettings.GetConfig("IocInCSharp/objects");
ObjectInfo info = cfgInfo.FindByName(name);
if(info != null)
{
assembly = Assembly.LoadFile(rootPath + info.assemblyName);
o = assembly.CreateInstance(info.typeName);
Type t = o.GetType();
for(int i=0; i<info.properties.Count; i++)
{
PropertyInfo prop = (PropertyInfo)info.properties[i];
assembly = Assembly.LoadFile(rootPath + prop.assemblyName);
p = assembly.CreateInstance(prop.typeName);
t.InvokeMember(prop.propertyName,
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.SetProperty, null, o, new Object[] {p});
}
}
return o;
}