5

Here is my Code. It's throwing exception "Object of type 'MyEnum.CommonCoreStandard' cannot be converted to type 'System.Nullable`1[System.Byte]'." while setting the value of second property "Prop2". I am using the concept of reflection to convert one type "BlEntity" into another type "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);
        }

    }
Vijay
  • 247
  • 1
  • 2
  • 11
  • 1
    See [this answer](http://stackoverflow.com/questions/3531318/convert-changetype-fails-on-nullable-types) – D Stanley Feb 14 '13 at 15:04
  • Also, "it's getting failure" is *never* enough detail. Please read http://tinyurl.com/so-list for a list of things to check when you're posting a question. – Jon Skeet Feb 14 '13 at 15:27
  • I simply used the statement "it's getting failure" as the complete code is available to look into. Now I made the correction and specified the complete exception message which is thrown. – Vijay Feb 14 '13 at 16:19
  • One more thing I would like to add is that, If I declare the first property "Prop1" as "DateTime?" then also the above code works for setting the value of "Prop1" but not for "prop2". So I believe there is something specific to "byte?" or nullable byte. – Vijay Feb 14 '13 at 16:25

1 Answers1

13
    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);
        }
    }
Vijay
  • 247
  • 1
  • 2
  • 11
  • I got hints for this answer is from the link "http://stackoverflow.com/questions/3531318/convert-changetype-fails-on-nullable-types" – Vijay Feb 14 '13 at 16:33