本文介绍了在静态类中使用TypeDescriptor.GetProperties的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我正在编写一个使用静态类存储所有应用程序设置的应用程序.我要做的是遍历静态设置类的属性,以显示给用户或写入xml文件以供以后检索.

我以前使用过System.ComponentModel.TypeDescriptor.GetProperties(this)来检索类的实例的属性.在MSDN中,它表示您可以为GetProperties的参数使用实例或类型.当我使用下面的代码时,PropertyDescriptorCollection为null.我的语法或方法有什么问题?

Hello,

I am writing an application that uses a static class to store all the apps settings. What I want to do is walk through the properties of the static settings class to display to the user or write to a xml file for retrieval later.

I have used System.ComponentModel.TypeDescriptor.GetProperties(this) to retrieve the properties of an instace of a class before. In MSDN it says that you can use an instance or the type for the parameter to GetProperties. When I use the code below I get null for the PropertyDescriptorCollection. What is wrong with my syntax or approach?

public static String GetProperties()
{
    String sReturn = string.Empty;  // Why have I used s prefix? C# has some variable naming conventions. Why didn't I follow them?
    System.ComponentModel.PropertyDescriptorCollection TypDecs;
    //Settings me = new Settings();
    //me = this;
    try
    {
        // TODO: this returns 0 properties because it is a static class
        //TypDecs = System.ComponentModel.TypeDescriptor.GetProperties(Type.GetType("Settings"));
        //TypDecs = System.ComponentModel.TypeDescriptor.GetProperties(T);
        //TypDecs = System.ComponentModel.TypeDescriptor.GetProperties(this);
        TypDecs = System.ComponentModel.TypeDescriptor.GetProperties(typeof DL_Batch_Import.Settings));
        foreach (System.ComponentModel.PropertyDescriptor Prop in TypDecs)
        {
            sReturn += Prop.DisplayName.ToString() + " = " + Prop.GetValue(Type.GetType("Settings")) + "/n";
        }
    }
    catch (Exception ex)
    {
        // WOW! Empty catch block!
    }
    return sReturn;
}


谢谢,

Michael Z


Thanks,

Michael Z

推荐答案




这篇关于在静态类中使用TypeDescriptor.GetProperties的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 01:58