如何将单一类型的类属性转换为该类型的列表

发布于 2025-01-25 20:37:05 字数 591 浏览 2 评论 0原文

给定以下类:

class MyClass {
    readonly MyType Object1 = new MyType { Prop1="Prop I want", Prop2="Prop I dont want"}
    readonly MyType Object2 = new MyType { Prop1="Prop I want", Prop2="Prop I dont want"}
    readonly MyType Object3 = new MyType { Prop1="Prop I want", Prop2="Prop I dont want"}
    readonly MyType Object4 = new MyType { Prop1="Prop I want", Prop2="Prop I dont want"}
    // etc, there are 100+ in this class, all of the type MyType
}

有没有办法将其变成类型myType或prop prop1的列表?

该类仅包含类型myType的属性。

Given the following class:

class MyClass {
    readonly MyType Object1 = new MyType { Prop1="Prop I want", Prop2="Prop I dont want"}
    readonly MyType Object2 = new MyType { Prop1="Prop I want", Prop2="Prop I dont want"}
    readonly MyType Object3 = new MyType { Prop1="Prop I want", Prop2="Prop I dont want"}
    readonly MyType Object4 = new MyType { Prop1="Prop I want", Prop2="Prop I dont want"}
    // etc, there are 100+ in this class, all of the type MyType
}

Is there a way I can turn this into a list of either the type MyType or the prop Prop1?

The class only contains properties of the type MyType.

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

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

发布评论

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

评论(1

一个人练习一个人 2025-02-01 20:37:05

为此,我们将使用 reflection 要获取prop1 myClass中所有字段的属性中的所有字符串列表。

鉴于您有一些myClass的实例,因此可以获取带有这样简单单线的字符串列表:

MyClass instance = new MyClass(); // This could of course be gotten from somewhere else.

List<string> prop1s = instance.GetType().GetRuntimeFields().Select(p => (MyType)p.GetValue(instance)).Select(p => p.Prop1).ToList();

如果您突然想在myType ,您可以简单地更改您在最后一个select()中选择的属性。

在这里,我们使用反射以获取所有字段(.getRuntimeFields()),myClass ,并且由于我们知道所有字段均为type myType 我们只能将所有字段投放到myType,然后只需选择prop1属性即可。最后,我们只是做tolist()将其作为列表,您当然可以将其用作iEnumerable&lt; string&gt;或一个数组,但希望这应该向您显示您如何获得想要作为列表的属性。

如果您只想拥有myType的列表,那么您可以跳过最后的select()这样:

List<MyType> myTypes = instance.GetType().GetRuntimeFields().Select(p => (MyType)p.GetValue(instance)).ToList();

To do this, we will use Reflection to get a list of all strings in the Prop1 properties of all fields in MyClass.

Given that you have some instance of MyClass, you can get the list of strings with a simple one-liner like this:

MyClass instance = new MyClass(); // This could of course be gotten from somewhere else.

List<string> prop1s = instance.GetType().GetRuntimeFields().Select(p => (MyType)p.GetValue(instance)).Select(p => p.Prop1).ToList();

If you suddenly want to get some other property inside of MyType, you can simply change which property you select in the last Select().

Here we use reflection to get all fields (.GetRunTimeFields()) inside of the type MyClass, and since we know that all fields are of type MyType we can just cast all of the fields to MyType and then simply select the Prop1 property. Finally we just do ToList() to have it as a list, you could of course use it as an IEnumerable<string> or an array, but hopefully this should show you how you get the property you wanted as as list.

If you instead just want to have a list of MyType, then you could skip the last Select() like this:

List<MyType> myTypes = instance.GetType().GetRuntimeFields().Select(p => (MyType)p.GetValue(instance)).ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文