将类型转换为 PropertyType
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
首先,而不是这样:
您可以这样做:
另外,如果(只是猜测)您通过 PropertyInfo 设置属性,则不需要强制转换:
其中变量值可以是对象类型,只有运行时类型必须匹配,您也可以这样检查:
不过,我想知道您想做什么。
First, instead of this:
you can do this:
also, you don't need to cast if (just guessing) you set a property via PropertyInfo:
where the variable value can be of type object, only the runtime type has to match, which you can also check like this:
Still, i'm wondering what you are trying to do.
对于
Type
所代表的含义可能存在一些误解。它仅代表类型对象,不代表其他任何东西。您的代码可以简化为以下内容,并且其行为完全相同:我的猜测是您想要返回
object
或类似的内容:这应该适合您。但我不知道你为什么要这么做。
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:My guess is that you want to return
object
, or something like that:This should work for you. But I have no idea why would you do that.
您需要更改实现以使用对象返回类型而不是 Type 作为返回类型,并且还必须使用可为空的日期时间类型。请在下面找到示例代码:
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:
当然,这是行不通的,因为您将类型与类型的值(或实例)混合在一起。
您可能需要研究的是
PropertyInfo.GetValue
(PropertyInfo.SetValue
可能同样相关,也取决于您下一步想要做什么) - 但我认为您可能需要准确考虑它是什么你想做;例如,在您的示例中,您可以只返回一个对象,或者可能是动态的,因为您直接实例化并处理该值。但听起来您想获取某物现有实例的值。假设您有一个
A
和一个B
,您想要获取Ba
的值并将Aa
设置为它,从你的解释中不清楚为什么你不能只做Ba = Aa
,或者鉴别器num
的正确用途;但如果您确实必须使用反射并且已经拥有PropertyInfo
,那么:尽管如此,这远非理想状态,而且大多存在缺陷如果不是矫枉过正的话——它希望有足够的信息让你把你能做的事情和你需要做的事情结合起来。
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 aB
, you want to get the value ofB.a
and setA.a
with it, it's not clear from your explanation why you can't just doB.a = A.a
, or what the discriminatornum
is properly for; but if you do have to use reflection and already have thePropertyInfo
, then: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.
您不需要反射来设置属性 Time 的类型。它被定义为 DateTime 类型。我不确定这是一个需要反思的问题。
另外,在
GetSomeType
方法中,您不需要实例化对象来检索其类型。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.