我正在尝试编写一个创建Type t对象并分配其属性的函数。
internal static object CreateInstanceWithParam(Type t, dynamic d)
{
//dynamic obj = t.GetConstructor(new Type[] { d }).Invoke(new object[] { d });
dynamic obj = t.GetConstructor(new Type[] { }).Invoke(new object[] { });
foreach (var prop in d.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public))
{
//prop.Name,
//prop.GetValue(d, null);
// assign the properties and corresponding values to newly created object ???
}
return obj;
}
然后我应该可以将它用于任何类型的类型,如
IUser user = (IUser)CreateInstanceWithParam(userType, new { UserID = 0, Name = "user abc", LoginCode = "abc", DefaultPassword = "xxxxxx" });
IUnit newUnit = (IUnit)CreateInstanceWithParam(unitType, new { ID = 3, Code = "In", Name = "Inch", Points = "72" })
如何将属性prop.Name
分配给obj
?
答案 0 :(得分:3)
假设您只是想复制属性,则根本不需要dynamic
:
internal static object CreateInstanceWithParam(Type type, object source)
{
object instance = Activator.CreateInstance(type);
foreach (var sourceProperty in d.GetType()
.GetProperties(BindingFlags.Instance |
BindingFlags.Public))
{
var targetProperty = type.GetProperty(sourceProperty.Name);
// TODO: Check that the property is writable, non-static etc
if (targetProperty != null)
{
object value = sourceProperty.GetValue(source);
targetProperty.SetValue(instance, value);
}
}
return instance;
}
答案 1 :(得分:1)
实际上,在这里使用dynamic
可能是坏的事情;您传入的对象是匿名类型的实例 - 不需要dynamic
。特别是,dynamic
成员访问与反射不同,并且您不能保证描述为dynamic
的对象将从.GetType().GetProperties()
返回任何有趣的内容;考虑ExpandoObject
等等。
但是,FastMember
(在NuGet上可用)可能很有用:
internal static object CreateInstanceWithParam(Type type, object template)
{
TypeAccessor target = TypeAccessor.Create(type),
source = TypeAccessor.Create(template.GetType());
if (!target.CreateNewSupported)
throw new InvalidOperationException("Cannot create new instance");
if (!source.GetMembersSupported)
throw new InvalidOperationException("Cannot enumerate members");
object obj = target.CreateNew();
foreach (var member in source.GetMembers())
{
target[obj, member.Name] = source[template, member.Name];
}
return obj;
}
特别是,这可以像使用反射API一样轻松地使用dynamic
API,并且您通常通常看到差异。