Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用PropertyInfo.SetValue方法(Object,Object)设置nullable属性的值_C#_Generics - Fatal编程技术网

C# 如何使用PropertyInfo.SetValue方法(Object,Object)设置nullable属性的值

C# 如何使用PropertyInfo.SetValue方法(Object,Object)设置nullable属性的值,c#,generics,C#,Generics,这是我的密码。设置第二个属性“Prop2”的值时,它引发异常“类型为'MyEnum.CommonCoreStandard'的对象无法转换为类型为'System.Nullable'1[System.Byte]”。我使用反射的概念将一种类型的“BlEntity”转换为另一种类型的“UiEntity” 公共类混合 { //CommonCoreStandard是一个枚举 公共CommonCoreStandard Prop1{get;set;} 公共CommonCoreStandard?Prop2{get

这是我的密码。设置第二个属性“Prop2”的值时,它引发异常“类型为'MyEnum.CommonCoreStandard'的对象无法转换为类型为'System.Nullable'1[System.Byte]”。我使用反射的概念将一种类型的“BlEntity”转换为另一种类型的“UiEntity”

公共类混合
{
//CommonCoreStandard是一个枚举
公共CommonCoreStandard Prop1{get;set;}
公共CommonCoreStandard?Prop2{get;set;}
}
公共类实体
{
公共字节Prop1{get;set;}
公共字节?Prop2{get;set;}
}
公营机构
{
var source=新的混合度(CommonCoreStandard.Keyideas和Details,CommonCoreStandard.Integrationof Knowledge);
var target=新的UiEntity();
TypeConverter.ConvertBlToUi(源、目标);
}
公共静态void ConvertBlToUi(TBl entitySource、TUi entityTarget)
{
var blProperties=typeof(TBl).GetProperties().Select(p=>new{Name=p.Name.ToLower(),Property=p}).ToArray();
var uiProperties=typeof(TUi).GetProperties().Select(p=>new{Name=p.Name.ToLower(),Property=p});
foreach(uiProperties中的uivar-uiProperty)
{
var value=blProperty.Property.GetValue(entitySource);
uiProperty.Property.SetValue(entityTarget,value);
}
}
公共静态void ConvertBlToUi(TBl entitySource、TUi entityTarget)
{
var blProperties=typeof(TBl).GetProperties().Select(p=>new{Name=p.Name.ToLower(),Property=p}).ToArray();
var uiProperties=typeof(TUi).GetProperties().Select(p=>new{Name=p.Name.ToLower(),Property=p});
foreach(uiProperties中的uivar-uiProperty)
{
var value=blProperty.Property.GetValue(entitySource);
var t=Nullable.GetUnderlineType(uiProperty.Property.PropertyType)??uiProperty.Property.PropertyType;
var safeValue=(value==null)?null:Convert.ChangeType(value,t);
uiProperty.Property.SetValue(entityTarget,safeValue);
}
}

另请参见,“it正在失败”这一细节永远不够详细。当你发布一个问题时,请阅读检查事项列表。我只是简单地使用了“it's getting failure”语句,因为完整的代码可以查看。现在我进行了更正并指定了抛出的完整异常消息。我想补充的是,如果我将第一个属性“Prop1”声明为“DateTime”,那么上面的代码也可以用于设置“Prop1”的值,但不能用于“prop2”。所以我相信有一些特定于“字节”或可空字节的东西。我从链接中得到了这个答案的提示“”
public class BlEntity
{
    //CommonCoreStandard is an enum
    public CommonCoreStandard Prop1 { get; set; }
    public CommonCoreStandard? Prop2 { get; set; }
}

public class UiEntity
{
    public byte Prop1 { get; set; }
    public byte? Prop2 { get; set; }
}

 public void ConvertBlToUi()
    {
        var source = new BlEntity(CommonCoreStandard.KeyIdeasAndDetails, CommonCoreStandard.IntegrationOfKnowledge);
        var target = new UiEntity();

        TypeConverter.ConvertBlToUi(source,target);
 }

    public static void ConvertBlToUi<TBl, TUi>(TBl entitySource, TUi entityTarget)
    {
        var blProperties = typeof(TBl).GetProperties().Select(p => new { Name = p.Name.ToLower(), Property = p }).ToArray();

        var uiProperties = typeof(TUi).GetProperties().Select(p => new { Name = p.Name.ToLower(), Property = p });

        foreach (var uiProperty in uiProperties)
        {
            var value = blProperty.Property.GetValue(entitySource);
            uiProperty.Property.SetValue(entityTarget, value);
        }

    }
    public static void ConvertBlToUi<TBl, TUi>(TBl entitySource, TUi entityTarget)
    {
        var blProperties = typeof(TBl).GetProperties().Select(p => new { Name = p.Name.ToLower(), Property = p }).ToArray();

        var uiProperties = typeof(TUi).GetProperties().Select(p => new { Name = p.Name.ToLower(), Property = p });

        foreach (var uiProperty in uiProperties)
        {
            var value = blProperty.Property.GetValue(entitySource);
            var t = Nullable.GetUnderlyingType(uiProperty.Property.PropertyType) ?? uiProperty.Property.PropertyType;
            var safeValue = (value == null) ? null : Convert.ChangeType(value, t);
            uiProperty.Property.SetValue(entityTarget, safeValue);
        }
    }