これが私のコードです。「'MyEnum.CommonCoreStandard' 型のオブジェクトは、'System.Nullable`1[System.Byte]' 型に変換できません」という例外がスローされます。2番目のプロパティ「Prop2」の値を設定しながら。リフレクションの概念を使用して、あるタイプ「BlEntity」を別のタイプ「UiEntity」に変換しています
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);
}
}