【问题标题】:Casting Type to PropertyType将类型转换为 PropertyType
【发布时间】:2011-12-26 08:56:24
【问题描述】:

我是 C# 反射的新手,有类似的东西:

class A
{
    DateTime _time = DateTime.Now;
    public DateTime Time
    {
       set
       {
          _time = value;
       }
       get
       {
          return _time;
       }
    }
}

这个方法在应用程序的某个地方:

public Type GetSomeType(int num)
{
    switch (num)
    {
        case 0:
            DateTime time = DateTime.Now;
            return time.GetType();
        case 1:
            int i = 5;
            return i.GetType();
        default:
            return null;
    }
}

我正在尝试使用 GetSomeType 方法的结果设置 A 类的 Time 属性:

A MyClass = new A();
MyClass.Time = (DateTime)GetSomeType(0); //Clearly, this does not work

我不知道这是否可能,还是我完全错了? 在我的实际应用程序中,由于我使用的是PropertyInfo,所以它更加复杂,但现在我很乐意掌握这个概念。

谢谢

于尔根

【问题讨论】:

  • 你为什么要这样做?必须有充分的理由使用反射。
  • 而您正在尝试将类型分配给 DateTime 属性,这没有任何意义。您想在这里解决哪个问题?
  • 你目前只是获取类型,我想你想获取属性的值?
  • 是的,我喜欢将返回的 Type 的值分配给属性。我不知道如何以通用方式做到这一点。

标签: c# reflection properties


【解决方案1】:

这自然是行不通的,因为您将类型与类型的值(或实例)混为一谈。

您可能需要研究的是PropertyInfo.GetValuePropertyInfo.SetValue 可能同样相关,这也取决于您接下来要做什么) - 但我认为您可能需要确切地考虑您的身份想要做;例如,在您的示例中,您可以只返回一个对象,或者可能是动态的,因为您直接实例化和处理该值。但听起来您想获取某物现有实例的值。

说如果你有一个A 和一个B,你想得到B.a 的值并用它设置A.a,你的解释不清楚为什么你不能只做@987654330 @,或鉴别器 num 的用途;但是如果你确实必须使用反射并且已经有了PropertyInfo,那么:

public dynamic GetSomeValue(object instance, PropertyInfo property)
{
    return property.GetValue(instance, null);
}

尽管这远非理想,而且即使不是矫枉过正,也大多存在缺陷 - 希望这将是足够的信息,让您能够将您可以做的事情与您需要做的事情结合起来。

【讨论】:

  • @leppie 我考虑过介绍两者,但是 OP 有一个强类型的 A 并希望 get B 的属性值。
  • @Mr 目前还不知道动态关键字。我想要做的就是让方法 GetSomeType(对不起,它应该被命名为 GetSomeValue) 返回某种类型的值,这样我就可以设置属性值:propInfo[0].SetValue(MyClass, GetSomeType(... ), 空)
  • @Juergen dynamic 允许类型根据在运行时分配给它的内容而成为不同的类型 - 我认为在这种情况下它除了 object 之外没有太多好处,但是这种情况有点毛茸茸的。不过,你为什么不直接做propInfo[0].SetValue(MyClass, DateTime.Now, null)
  • 我无法设置具体的 DateTime 对象,因为在运行时我不是实际类型。但我现在明白你的意思了:我可以将 propertyInfo 作为参数传递给 GetSomeType 方法,并在正确的 switch 语句中分配值。对不起,代码 sn-p,但真正的代码太长了。
【解决方案2】:

您不需要反射来设置属性时间的类型。它被定义为一种日期时间。我不确定这是反射的问题。

同样在GetSomeType 方法中,您不需要实例化对象来检索它们的类型。

public Type GetType(int num)
{
  switch(num)
  {
     case 0:
       return typeof(DateTime);
     case 1:
       return typeof(int)
   }

  return null;
}

【讨论】:

    【解决方案3】:

    首先,不要这样:

    DateTime time = DateTime.Now;
    return time.GetType();
    

    你可以这样做:

    return typeof(DateTime);
    

    此外,如果(只是猜测)您通过 PropertyInfo 设置属性,则不需要强制转换:

    propInfo.SetValue(instance, value, null);
    

    变量值可以是对象类型,只有运行时类型必须匹配,你也可以这样检查:

    if (value == null || propInfo.PropertyType == value.GetType())  
    

    不过,我想知道您要做什么。

    【讨论】:

      【解决方案4】:

      对于Type 所代表的含义可能存在一些误解。它只代表类型对象,没有别的。您的代码可以简化为以下内容,其行为完全相同:

      public Type GetSomeType(int num)
      {
          switch (num)
          {
              case 0:
                  return typeof(DateTime);
              case 1:
                  return typeof(int);
              default:
                  return null;
          }
      }
      

      我的猜测是你想返回object,或者类似的东西:

      public object GetSomeType(int num)
      {
          switch (num)
          {
              case 0:
                  return DateTime.Now;
              case 1:
                  return 5;
              default:
                  return null;
          }
      }
      

      这应该适合你。但我不知道你为什么要这么做。

      【讨论】:

        【解决方案5】:

        您需要更改您的实现以使用对象返回类型而不是 Type 作为返回类型,并且您还必须使用可为空的日期时间类型。请在下面找到示例代码:

        class A
        {
            DateTime? _time = DateTime.Now;
            public DateTime? Time
            {
               set
               {
                  _time = value;
               }
               get
               {
                  return _time;
               }
            }
        }
        
        
        public object GetSomeType(int num)
        {
            switch (num)
            {
                case 0:
                    DateTime time = DateTime.Now;
                    return time;
                case 1:
                    int i = 5;
                    return i;
                default:
                    return null;
            }
        }
        
        A MyClass = new A();
        MyClass.Time = this.GetSomeType(0) as DateTime?; //This should now work
        

        【讨论】:

          猜你喜欢
          • 2022-11-18
          • 1970-01-01
          • 2014-02-20
          • 2014-09-02
          • 2011-09-12
          • 1970-01-01
          • 2019-06-22
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多