在静态类中使用TypeDescriptor.GetProperties [英] Using TypeDescriptor.GetProperties with Static Class

查看:58
本文介绍了在静态类中使用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

推荐答案

您的DL_Batch_Import.Settings类型工具是否实现IComponent?

阅读有关 TypeDescriptor [
也许TypeDescriptor 需要要检查的类型以实现IComponent?我尚未尝试过,但这可能是研究的方向.
Does your DL_Batch_Import.Settings type implement IComponent?

Reading the MSDN library article for TypeDescriptor[^] - in particular the Remarks section, it appears that TypeDescriptor is intended for use on classes that implement IComponent: "In contrast [to System.Reflection.Type], TypeDescriptor is an extensible inspection mechanism for components: those classes that implement the IComponent interface." (2nd paragraph).

Perhaps TypeDescriptor requires the types being inspected to implement IComponent? I''ve not tried it, but it''s perhaps a direction to investigate.


因此,由于我没有使用TypeDescriptor,所以我只是在猜测. ..但我怀疑它不会告诉您有关static属性的任何信息.从我阅读的内容来看,它的存在是为了支持从事COM工作的人们,其中某些类型的部分可能不是静态已知的(即在编译时就知道的).但是我什至不确定.

从可能与COM相关"推断出的有用之处在于,COM对象/接口与实例相关,因此public static anything ...不太可能起作用,因为static关键字使属性(或字段)成为类型的属性,而不是类型的实例.

-您需要使用TypeDescriptor.GetProperties(), 还是可以使用Type.GetProperties()?如果可以使用后者,则可以同时发现实例和静态字段和/或属性(和方法).

-如果必须使用TypeDescriptor,是否可以将Settings对象更改为类型的单例实例,而不是静态类型的静态属性?

关于VB.Net与C#-我怀疑问题是由于VB.Net和C#之间存在某些内在差异,但由于了解了TypeDesriptor的工作原理……我希望当它开始工作时,您可以能够使其在两种语言中都能正常工作.您是否遇到过
Reflector [myprop声明为实例字段,但将MyProp声明为静态属性:由于静态属性不知道,因此不会编译该字段关于实例内部的任何事情.
So, since I''ve not used TypeDescriptor, I''m only guessing ... but I suspect that it won''t tell you anything about static properties. From the little I''ve read, it exists to support people doing COM work, where parts of a type may not be statically known (i.e. known at compile time). But I''m not even sure of that.

The useful thing to infer from ''maybe its related to COM'' is that a COM object / interface is related to an instance, so public static anything ... is unlikely to work because the static keyword makes the property (or field) a property of the type, not of an instance of the type.

- Do you need to use TypeDescriptor.GetProperties(), or can you use Type.GetProperties()? If you can use the latter you can discover both instance and static field and/or properties (and methods).

- If you must use TypeDescriptor, can you change your Settings object to be a singleton instance of a type, instead of being the static properties of a static type?

Re VB.Net vs C# - I doubt the problem is due to some intrinsice difference between VB.Net and C#, but to understanding how TypeDesriptor works ... I expect that when you get it to work you'''' be able to make it work in both languages. Have you come across Reflector[^]? It''s a free reverse-engineering tool for .Net assemblies. It generates C# (or VB.Net) source from the IL in an assembly. So, when you get your problem working in one language, you can use Reflector to see how it would look in the other.

In your ''What I should be doing'' section, note that you''re declaring myprop as an instance field, but MyProp as a static property: this will not compile because the static property doesn''t know anything about the internals of instances.


我只是重新阅读了您对问题的开头描述.有没有原因导致您无法使用.Net的内置设置基础结构?

该基础结构已经可以读取/写入设置的XML文件,对于简单的情况,您可以让Visual Studio中的设计人员为您生成文件和设置类(转到程序集的属性"页面,选择设置"选项卡,然后声明设置的名称和类型.

如果您需要变得比名称/值对更复杂,那么System.Configuration命名空间中的类可能仍会满足您的需求.
I''ve just re-read you opening description of the problem. Is there a reason why you are not able to use .Net''s built-in settings infrastructure?

That infrastructure already reads from / writes to an XML file of settings, and for simple cases you can let the designer in Visual Studio generate the file and Settings class for you (go to the assembly''s Properties page, pick the Settings tab and declare the names and types of the settings.

If you need to get more sophisticated than name/value pairs, then perhaps the classes in the System.Configuration namespace will still meet your need.


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

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