首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试让TypeDescriptor.GetProperties正常工作

尝试让TypeDescriptor.GetProperties正常工作
EN

Stack Overflow用户
提问于 2017-07-19 17:12:35
回答 1查看 275关注 0票数 0

真正开始使用C#。我正在尝试实现TypeDescriptor.GetProperties。在代码中,我不断得到一个空集合。我不知道为什么。

任何帮助都是非常感谢的。谢谢。

代码语言:javascript
复制
public class SampleObjectToExportI
{
    public Guid Id;
    public DateTime Date;
    public string StringValue;
    public int NumberValue;
    public bool BooleanValue;
    public SampleObjectToExportI(Guid id, DateTime date, string stringValue, int numberValue, bool booleanValue)
    {
        this.Id = id;
        this.Date = date;
        this.StringValue = stringValue;
        this.NumberValue = numberValue;
        this.BooleanValue = booleanValue;
    }
}
class Program
{
    static void Main(string[] args)
    {
        var myList = new List<SampleObjectToExportI>();
        myList.Add(new SampleObjectToExportI(Guid.NewGuid(), DateTime.Now, "String4", 400, true));
        myList.Add(new SampleObjectToExportI(Guid.NewGuid(), DateTime.Now, "String5", 500, false));
        myList.Add(new SampleObjectToExportI(Guid.NewGuid(), DateTime.Now, "String6", 600, true));

        foreach (var sampleObjectToExport in myList)
        {
            foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(sampleObjectToExport))
            {
                string name = descriptor.Name;
                object value = descriptor.GetValue(sampleObjectToExport);
                Console.WriteLine("{0}={1}", name, value);
            }
            //Console.WriteLine(sampleObjectToExport.NumberValue);
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-19 17:14:44

你没有属性,如果你想要的话语法是这样的:

代码语言:javascript
复制
public Guid Id { set; get; }
public DateTime Date { set; get; }
public string StringValue { set; get; }
public int NumberValue { set; get; }
public bool BooleanValue { set; get; }

有关更多信息,请参阅documentation

另一种方法是保持代码的原样,并使用Type.GetFeilds()

代码语言:javascript
复制
foreach (var fieldInfo in typeof(SampleObjectToExportI).GetFields())
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45186008

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档