如果检测类属性是引用类型 [英] Detecting if class property is a reference type

查看:146
本文介绍了如果检测类属性是引用类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在类的属性看,以检测其中是否是引用类型时,是否有可能

Is it possible when looking at a class' properties to detect if any of them is a reference type.

下面以一个例子:

public class Client
{
   public int Id { get; set; }
   public string Name { get; set; }
}

public class ProgrammeClient
{
    public int Id { get; set; }
    public bool IsActive { get; set; }
    public IClient Client { get; set; }
}



ProgrammeClient: - 结果
Id和IsActive是属性但客户端是引用类型。有没有检测这个办法吗?

ProgrammeClient: -
Id and IsActive are properties but Client is a reference type. Is there a way of detecting this?

非常感谢,
钢钣。

Many thanks, Kohan.

原因我想问的是:我使用的检查类型匹配属性名称和复制值之前相同的一个映射。我的希望是,检测类和覆盖类型匹配,简单地复制类的属性,如果他俩类型相匹配。

The reason i ask is: I am using a mapper that checks types are the same before matching property names and copying the values. My hope is to detect classes and override the type matching and simply copy the classes properties if the THEY type match.

推荐答案

好了,这听起来像你的可能的试图检测值类型和引用类型之间的区别。你可以发现,从使用 Type.IsValueType ...但要注意,值类型可以轻松拥有的属性了。 (想想的DateTime 为例)此外,某些类型的,你可能要视为没有对象是引用类型 - 字符串是一个典型的例子。

Well, it sounds like you may be trying to detect the difference between a value type and a reference type. You can find that out using Type.IsValueType... but be aware that value types can easily have properties too. (Think about DateTime for example.) Also, some types which you may want to regard as "not objects" are reference types - string being a prime example.

另一种选择是使用的 Type.IsPrimitive - 是的的东西你要找的?如果是这样,你应该知道,小数的DateTime 字符串是的的基本类型。

Another option would be to use Type.IsPrimitive - is that what you're looking for? If so, you should be aware that decimal, DateTime and string are not primitive types.

如果你能描述的究竟的是什么使一个类型的对象,在你的思维方式(或者说,以任何方式使得你想与你的类型做一个语义差别)。我怀疑你目前还没有一个非常明确的一套标准 - 未来与这些标准可能澄清你的当前任务的其他方面也一样。

If you can describe exactly what makes a type an "object" in your way of thinking (or rather, in whatever way makes a semantic difference in what you're trying to do with your type). I suspect you don't currently have a very clear set of criteria - coming up with those criteria may well clarify other aspects of your current task, too.

这篇关于如果检测类属性是引用类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆