'casting' with reflection

前端 未结 6 1818
面向向阳花
面向向阳花 2020-11-30 21:47

Consider the following sample code:

class SampleClass
{
    public long SomeProperty { get; set; }
}

public void SetValue(SampleClass instance, decimal valu         


        
6条回答
  •  攒了一身酷
    2020-11-30 22:18

    When the Type is a Nullable Guid then none of the above proposed solutions work. Invalid cast from 'System.DBNull' to 'System.Guid' exception is thrown at Convert.ChangeType

    To fix that change to:

    var convertedValue = value == System.DBNull.Value ? null : Convert.ChangeType(value, targetType);
    

提交回复
热议问题