加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 检查PropertyInfo.SetValue是否会抛出ArgumentException

发布时间:2020-12-15 07:44:51 所属栏目:百科 来源:网络整理
导读:我继承了一些尝试设置属性的代码: object target = ... // some instance on which we want to set a propertyobject value = ... // some value - in this case,a stringvar propertyInfo = ... // some property of target - in this case,not a stringtr
我继承了一些尝试设置属性的代码:
object target = ...    // some instance on which we want to set a property
object value = ...     // some value - in this case,a string
var propertyInfo = ... // some property of target - in this case,not a string

try
{
    propertyInfo.SetValue(obj,value,null);
}
catch (ArgumentException)
{
    // We go off and look for our own way of converting between
    // the type of value and the type of the property.
}

在当前使用中,异常被捕获并抛出很多,所以我想首先进行检查:

if (propertyInfo.PropertyType.IsAssignableFrom(value.GetType())
{
    // Try/catch as above
}
else
{
    // Do the manual conversion as if the exception had been thrown.
}

这运行得更快.但是,我唯一担心的是,IsAssignableFrom可能会为某些类型返回false,而SetValue实际上会成功. (这会导致我们在不需要时查找手动转换,并且可能无法完全设置值.)

SetValue的规范说

value cannot be converted to the type of PropertyType

这与可分配性不完全相同.

(如果在SetValue失败的情况下IsAssignableFrom返回true,那很好 – 手动转换仍然会发生.)

有人可以向我确认这是否可行?

解决方法

正如您所怀疑的那样,short类型的值可以通过反射分配给int类型的属性,尽管typeof(int).IsAssignableFrom(typeof(short))返回false.

查看ArgumentException的stacktrace:

at System.RuntimeType.TryChangeType(Object value,Binder binder,CultureInfo culture,Boolean needsSpecialCast)
  at System.RuntimeType.CheckValue(Object value,BindingFlags invokeAttr)
  at System.Reflection.MethodBase.CheckArguments(Object[] parameters,BindingFlags invokeAttr,Signature sig)
  at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj,Object[] parameters,CultureInfo culture)
  at System.Reflection.RuntimeMethodInfo.Invoke(Object obj,CultureInfo culture)
  at System.Reflection.RuntimePropertyInfo.SetValue(Object obj,Object value,Object[] index,Object[] index)
  at System.Reflection.PropertyInfo.SetValue(Object obj,Object value)
  at ConsoleApplication3.Program.Main(String[] args) in ...

RuntimeType.TryChangeType中抛出异常.查看mscorlib中的源代码:

// System.RuntimeType
[SecurityCritical]
private object TryChangeType(object value,bool needsSpecialCast)
{
    ...
    if (this.IsInstanceOfType(value))
    {
      return value;
    }
    ...
      if (RuntimeType.CanValueSpecialCast(valueType,this))
      {
    ...
  }
  throw new ArgumentException(string.Format(CultureInfo.CurrentUICulture,Environment.GetResourceString("Arg_ObjObjEx"),value.GetType(),this));
}

不幸的是,有很多内部函数被调用来确定是否应该抛出ArgumentException.我怀疑原语的转换是在这个函数周围处理的:

// System.RuntimeType
[SecurityCritical]
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern bool CanValueSpecialCast(RuntimeType valueType,RuntimeType targetType);

System.RuntimeType.CheckValue(object,Binder,CultureInfo,BindingFlags)中也有一些有趣的代码:

bool flag = base.IsPointer || this.IsEnum || base.IsPrimitive;
if (flag)
{
  ...
  if (RuntimeType.CanValueSpecialCast(valueType,this))
  {
    ...
  }
}

所以它看起来像指针,枚举和原始类型以不同的方式处理.我不鼓励你试着复制那个逻辑.使用IsAssignableFrom可能更容易,并分别处理整数类型.处理指针,枚举和原始类型参数看起来非常复杂,我会回避尝试捕获那里.但是,如果发生参数错误,则可以缓存,因此对具有相同参数类型的同一方法的后续调用可能会更快一些.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读