将类型转换为 PropertyType

发布于 2024-12-14 19:24:48 字数 896 浏览 1 评论 0原文

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

我想要做的是使用 Time 的结果设置类 A 的 Time 属性code>GetSomeType 方法:

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

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

谢谢于

尔根

I am new to reflection in C# and have something similar like this:

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:

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.

Thanks

Juergen

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

混吃等死 2024-12-21 19:24:49

首先,而不是这样:

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

您可以这样做:

return typeof(DateTime);

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

propInfo.SetValue(instance, value, null);

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

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

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

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:

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:

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

Still, i'm wondering what you are trying to do.

等风也等你 2024-12-21 19:24:49

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

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

There's probably some misunderstanding of what Type represents. 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:

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.

感情废物 2024-12-21 19:24:49

您需要更改实现以使用对象返回类型而不是 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

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. 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
丑丑阿 2024-12-21 19:24:48

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

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

假设您有一个 A 和一个 B,您想要获取 Ba 的值并将 Aa 设置为它,从你的解释中不清楚为什么你不能只做 Ba = Aa ,或者鉴别器 num 的正确用途;但如果您确实必须使用反射并且已经拥有PropertyInfo,那么:

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

尽管如此,这远非理想状态,而且大多存在缺陷如果不是矫枉过正的话——它希望有足够的信息让你把你能做的事情和你需要做的事情结合起来。

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; 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 B.a and set A.a with it, it's not clear from your explanation why you can't just do B.a = A.a, or what the discriminator num is properly for; but if you do have to use reflection and already have the PropertyInfo, then:

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.

心凉怎暖 2024-12-21 19:24:48

您不需要反射来设置属性 Time 的类型。它被定义为 DateTime 类型。我不确定这是一个需要反思的问题。

另外,在 GetSomeType 方法中,您不需要实例化对象来检索其类型。

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

  return null;
}

You don't need reflection to set the type of the property Time. it's defined as a type of 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.

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

  return null;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文