C# ';System.Boolean';无法转换为类型';System.Reflection.RuntimePropertyInfo';
我有一个动态创建的类。我有另一个现有的类,它有数据,所以我试图将现有的类数据映射到动态创建的类属性 但动态类中的所有文件都将其类型显示为System.Reflection.RuntimePropertyInfo 有人能帮助我理解为什么动态类属性显示为System.Reflection.RuntimePropertyInfo,即使我们在创建时添加了指定的类型C# ';System.Boolean';无法转换为类型';System.Reflection.RuntimePropertyInfo';,c#,reflection.emit,system.reflection,C#,Reflection.emit,System.reflection,我有一个动态创建的类。我有另一个现有的类,它有数据,所以我试图将现有的类数据映射到动态创建的类属性 但动态类中的所有文件都将其类型显示为System.Reflection.RuntimePropertyInfo 有人能帮助我理解为什么动态类属性显示为System.Reflection.RuntimePropertyInfo,即使我们在创建时添加了指定的类型 public object FillData<TresultType>(TresultType result)
public object FillData<TresultType>(TresultType result)
{
PropertyInfo[] pi = result.GetType().GetProperties();
foreach (var property in pi)
{
var TargetProperty = this.GetType().GetProperty(property.Name);
if (TargetProperty!=null)
{
TargetProperty.SetValue( this, property.GetValue(result, null), null );
}
}
return this;
}
我的问题是无法将现有的类属性类型(此处为布尔值)转换为显示为System.Reflection.RuntimePropertyInfo的目标属性类型
这是我创建动态对象的函数
public object GetViewModel<TresultType, TviewModelType>(TresultType result, TviewModelType ViewModel)
{
if (DynamicType.asmBuilder == null)
DynamicType.GenerateAssemblyAndModule();
var finalType = DynamicType.modBuilder.GetType("Beacon11");
TypeBuilder tb = DynamicType.CreateType(DynamicType.modBuilder, ViewModel.GetType().ToString());
tb.SetParent(typeof(ResultViewModelVersionable));
var sourceType = result.GetType();
var targetType = tb.GetType();
foreach (var property in sourceType.GetProperties())
{
var targetProperty = targetType.GetProperty(property.Name);
if (targetProperty == null)
{
DynamicType.CreateProperty(tb, property.Name, property.GetType());
}
}
finalType = tb.CreateType();
var Methods = tb.GetMethods();
Object obj = Activator.CreateInstance(finalType);
return obj;
}
请让我知道我是否可以遵循任何不同的方法。
当做
Mohan如果
PropertyInfo.PropertyType==typeof(System.Reflection.RuntimePropertyInfo)
则您的属性具有typeSystem.Reflection.RuntimePropertyInfo
您说您已经动态地创建了这个类型(可能使用反射发射)。该代码中可能存在错误。错误如下:
foreach (var property in sourceType.GetProperties())
{
var targetProperty = targetType.GetProperty(property.Name);
if (targetProperty == null)
{
DynamicType.CreateProperty(tb, property.Name, property.GetType());
}
}
您正在向CreateProperty
方法传递一种类型的RuntimePropertyInfo
。试试这个:
foreach (var property in sourceType.GetProperties())
{
var targetProperty = targetType.GetProperty(property.Name);
if (targetProperty == null)
{
DynamicType.CreateProperty(tb, property.Name, property.PropertyType);
}
}
无法对此进行测试,但我认为这应该可以解决问题 错误发生在哪一行?运行时什么是TargetProperty.PropertyType?下面的一行抛出异常“TargetProperty.SetValue(this,property.GetValue(result,null),null)”;尝试这样强制转换:TargetProperty.SetValue(this,(TresultType)property.GetValue(result,null),null);TargetProperty类型显示为System.Reflection.RuntimePropertyInfo。所以我希望上述解决方案不会解决我的问题。因为TresultType已经是布尔型的了,我也不知道为什么会投反对票。向上投票反对一些坏的juju。所以最近有点脱离了这个链条。这不是一个被问了无数次的noob问题。起初你本可以提供更多的信息,但我觉得这个问题本身是合理的。这里还有很多“专家”,他们似乎觉得如果一个问题不值得一篇博士论文考虑,就应该在没有任何解释的情况下否决它。非常感谢。在通过property type时,我使用了以下行property.getType()而不是“property.PropertyType”。这解决了上述函数DynamicType.CreateProperty(tb,property.Name,property.GetType())中的以下代码中的问题;应重写为DynamicType.CreateProperty(tb,property.Name,property.PropertyType);
foreach (var property in sourceType.GetProperties())
{
var targetProperty = targetType.GetProperty(property.Name);
if (targetProperty == null)
{
DynamicType.CreateProperty(tb, property.Name, property.GetType());
}
}
foreach (var property in sourceType.GetProperties())
{
var targetProperty = targetType.GetProperty(property.Name);
if (targetProperty == null)
{
DynamicType.CreateProperty(tb, property.Name, property.PropertyType);
}
}