获取值的集合,从结构的常量属性 [英] Get collection of values from struct's const properties

查看:127
本文介绍了获取值的集合,从结构的常量属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个结构,看起来像这样:

 公共结构MYSTRUCT 
{
公共常量字符串Property1 =等等等等等等;
公共常量字符串Property2 =富;
公共常量字符串Property3 =酒吧;
}



我想以编程方式检索MYSTRUCT的常量属性的值的集合。
到目前为止,我没有成功尝试这样的:

  VAR X =从typeof运算(MYSTRUCT)D。的GetProperties()
选择d.GetConstantValue();



任何人有什么想法?谢谢



修改:这是什么最终为我工作:



<$ P $在typeof运算p> 从D(MYSTRUCT).GetFields()
选择d.GetValue(新MYSTRUCT());

感谢您乔纳森·亨森和JaredPar您的帮助!


解决方案

这些领域而不是属性,因此你需要使用 GetFields

  VAR X =从typeof运算D(MYSTRUCT).GetFields()
选择d.GetRawConstantValue();



此外,我相信你正在寻找的方法 GetRawConstantValue 而不是 GetConstantValue


I've got a struct that looks like this:

public struct MyStruct
{
    public const string Property1 = "blah blah blah";
    public const string Property2 = "foo";
    public const string Property3 = "bar";
}

I want to programmatically retrieve a collection of MyStruct's const properties' values. So far I've tried this with no success:

var x = from d in typeof(MyStruct).GetProperties()
                    select d.GetConstantValue();

Anyone have any ideas? Thanks.

EDIT: Here is what eventually worked for me:

from d in typeof(MyStruct).GetFields()
select d.GetValue(new MyStruct());

Thank you Jonathan Henson and JaredPar for all your help!

解决方案

These are fields not properties and hence you need to use the GetFields method

    var x = from d in typeof(MyStruct).GetFields()
            select d.GetRawConstantValue();

Also I believe you're looking for the method GetRawConstantValue instead of GetConstantValue

这篇关于获取值的集合,从结构的常量属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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