简体   繁体   English

将类型转换为PropertyType

[英]Casting Type to PropertyType

I am new to reflection in C# and have something similar like this: 我是C#中的反射新手,并且具有类似以下内容:

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

And this method somewhere in the app: 而此方法在应用程序中的某个位置:

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;
    }
}

What I'm trying to do is set the Time property of class A with the result of the GetSomeType method: 我想要做的是使用GetSomeType方法的结果设置类A的Time属性:

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

I don't know if this is possible at all or am I totally wrong here? 我不知道这是否完全可能,或者我在这里完全错了吗? In my real application it is more complex since I'm working with PropertyInfo , but for now I would be happy to grasp the concept. 在我的实际应用程序中,由于我正在使用PropertyInfo ,所以它更加复杂,但是现在我很乐意掌握这个概念。

Thanks 谢谢

Juergen 于尔根

Naturally this isn't going to work because you're mixing up types with the value (or instances) of types. 自然这是行不通的,因为您将类型与类型的值(或实例)混合在一起。

What you will likely need to look into is PropertyInfo.GetValue ( PropertyInfo.SetValue might be just as relevant, depending on what you want to do next, too) - but I think you might need to consider exactly what it is you want to do; 您可能需要研究的是PropertyInfo.GetValuePropertyInfo.SetValue可能同样相关,这取决于下一步要执行的操作)-但我认为您可能需要仔细考虑要执行的操作; in your example, for instance, you could just return an object, or perhaps dynamic since you instantiate and handle the value directly. 例如,在您的示例中,您可以只返回一个对象,或者返回动态对象,因为您直接实例化并处理该值。 But it sounds like you want to get the value of an existing instance of something. 但这听起来像您想获取某个事物的现有实例的价值。

Say if you have an A and a B , you want to get the value of Ba and set Aa with it, it's not clear from your explanation why you can't just do Ba = Aa , or what the discriminator num is properly for; 假设您有一个A和一个B ,您想要获得Ba的值并设置Aa ,那么从您的解释中并不清楚为什么您不能只做Ba = Aa ,或者鉴别num适用于什么; but if you do have to use reflection and already have the PropertyInfo , then: 但如果必须使用反射已经拥有的PropertyInfo ,则:

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

Although, this isn't anywhere near ideal, and mostly flawed if not overkill - it will hopefully be sufficient information to allow you to marry up what you can do with what you need to do. 尽管这并不是理想的方法,而且即使不是矫kill过正,也大多存在缺陷-希望它将有足够的信息来使您将可以做的事与需要做的事结合起来。

You don't need reflection to set the type of the property Time. 您不需要反射即可设置属性“时间”的类型。 it's defined as a type of DateTime. 它定义为DateTime类型。 I'm not sure that this is a problem for reflection. 我不确定这是否值得反思。

Also in the GetSomeType method you don't need to instantiate objects to retrieve their type. 同样,在GetSomeType方法中,您无需实例化对象即可检索其类型。

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

  return null;
}

There's probably some misunderstanding of what Type represents. Type代表什么可能会有误解。 It represents only the type object, nothing else. 它仅代表类型对象,没有别的。 Your code could be simplified to the following and it would behave exactly the same: 您的代码可以简化为以下内容,并且行为完全相同:

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

My guess is that you want to return object , or something like that: 我的猜测是您想返回object或类似的东西:

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

This should work for you. 这应该为您工作。 But I have no idea why would you do that. 但是我不知道你为什么要这么做。

First, instead of this: 首先,代替此:

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

you can do this: 你可以这样做:

return typeof(DateTime);

also, you don't need to cast if (just guessing) you set a property via PropertyInfo: 同样,如果(只是猜测)您通过PropertyInfo设置了属性,则无需强制转换:

propInfo.SetValue(instance, value, null);

where the variable value can be of type object, only the runtime type has to match, which you can also check like this: 其中变量值可以是object类型,只有运行时类型必须匹配,您还可以像这样检查:

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

Still, i'm wondering what you are trying to do. 不过,我想知道您正在尝试做什么。

You would need to change your implementation to use object return type instead of Type as return type and also you would have to use nullable datetime type. 您将需要更改实现,以使用对象返回类型而不是Type作为返回类型,并且还必须使用可为null的datetime类型。 Please find below the sample code: 请在下面的示例代码中找到:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM