如何不反射获取类中指定类型的所有属性

发布于 2024-12-23 03:06:34 字数 441 浏览 3 评论 0原文

我有一个具有大量属性的类。其中大多数是定制类型。我想获取所有这些属性,其接口类型相同。

Public class abc:IamLegend
{
    few properties
}

public class def:IamLegend
{
    few properties
}

public class on_which_iamworking
{
    public abc propabc{ get; set; }
    public def propdef{ get; set; }
    public someothertype propother{ get; set; }
}

我想要返回 propabc 和 propdef 的东西。

我知道如何使用反射来做到这一点,但我正在寻找另一种方法。

我正在研究 c# 4.0

谢谢

I have a class which is has tons of properties. Most of them are of custom types. I want to get all those properties, type of whose interface is same.

Public class abc:IamLegend
{
    few properties
}

public class def:IamLegend
{
    few properties
}

public class on_which_iamworking
{
    public abc propabc{ get; set; }
    public def propdef{ get; set; }
    public someothertype propother{ get; set; }
}

I want something which returns propabc and propdef.

I know how to do it using reflection, but I am looking for another way.

I am working on c# 4.0

Thanks

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

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

发布评论

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

评论(4

若有似无的小暗淡 2024-12-30 03:06:34

恐怕如果不使用反射,这在运行时是不可能的。这就是反射的设计目的。

I am afraid that this is not possible at runtime without using reflection. That's what reflection is designed for.

没有你我更好 2024-12-30 03:06:34

反射的主要问题是速度慢。如果您不想仅因为反射速度慢而使用反射,则可以在某些静态属性或类中缓存属性列表。我在类似问题中广泛使用了这种技术,并且没有任何性能问题。

如果您对反射有圣战,您可以创建一个特殊的实用程序来解析 C# 文件(或构建您的项目,加载输出程序集并使用反射,但仅在构建之前,而不是在运行时),找到所需的属性并将其写入自动生成的文件(也可能是 C# 代码文件)作为静态类数组属性初始值设定项。并在项目的预构建事件中调用该 util。然后你将完全获得所有需要的属性,而无需反射=)(但我不会这样做)

The main problem of reflection is that it is slow. If you don't want to use reflection only because of it's slowness, you could make caching of your property list in some static property or class. I used this tecknique widely in similar problems and there wasn't any problems with perfomance.

If you have holy war against reflection, you could create a special util that parses C# file (or builds your prokects, loads output assembly and use reflection, but only before build, not in run-time), finds needed properties and writes it into autogenerated file (maybe also C# code file) as static-class array-property initializer. And call that util on pre-build event of your project. Then you'll get all needed properties completely without reflections =) (but I wouldn't do that)

救赎№ 2024-12-30 03:06:34

嗯,有两种方法:

1/

 return new List<string> { "propabc", "propdev" };

2/ 反射 :P

如果您需要多次检索属性列表并且担心性能影响,请只计算一次列表并将其存储在静态属性中(作为属性列表类的属性在运行时不会改变)。

Well, there's two ways:

1/

 return new List<string> { "propabc", "propdev" };

2/ Reflection :P

If you need to retrieve the list of properties many times and are afraid of the performance impact, compute the list only once and store it in a static property (as the list of properties of a class won't change during runtime).

御弟哥哥 2024-12-30 03:06:34

对于组件还有一种替代方法。它是实现 IComponent 的类的 TypeDescriptor。我相信WPF 使用的是它。

There is an alternative approach for components. It is TypeDescriptor for classes that implement IComponent. I believe that is used by WPF.

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