C# 使用TypeDescriptor在WCF服务中加载自定义属性

C# 使用TypeDescriptor在WCF服务中加载自定义属性,c#,wcf,custom-attributes,typedescriptor,C#,Wcf,Custom Attributes,Typedescriptor,我有几个具有以下datacontract定义和元数据类的类 [MetadataType(typeof(ClassAMetadata))] public partial class ClassA { public string PropertyA { get; set; } } public class ClassAMetadata { [CustomAttribute(Title = "PropertyAText")] public string PropertyA {

我有几个具有以下datacontract定义和元数据类的类

[MetadataType(typeof(ClassAMetadata))]
public partial class ClassA
{
    public string PropertyA { get; set; }
}

public class ClassAMetadata
{
    [CustomAttribute(Title = "PropertyAText")]
    public string PropertyA { get; set; }
}
然后,我希望使用以下代码从当前程序集获取所有该程序集类型中使用的CustomAttribute的属性标题:

internal static Dictionary<string, string> GetCustomAttributeTitles<T>(this T entity) where T : class
{          
    Dictionary<string, string> result = (from prop in TypeDescriptor.GetProperties(entity.GetType()).Cast<PropertyDescriptor>()
                                            from att in prop.Attributes.OfType<CustomAttribute>()
                                            select new { prop.Name, att.Title }).ToDictionary(t => t.Name, t => t.Title);

    return result;
}
我的计划的一个例子是:

InstallForAssembly(Assembly.LoadFile(Environment.CurrentDirectory + "\\DataContract.dll"));
ClassA instanceOfA = new ClassA();
//fill instanceOfA
var dictionary = instanceOfA.GetCustomAttributeTitles();
//do something with dictionary

这在控制台应用程序中运行良好,但在WCF服务方法中不起作用,GetCustomAttributeTitles方法中的字典总是空的。为什么会发生这种情况?

我想不出任何特定的原因,这在WCF中不起作用,除了类型描述符提供程序的注册(在控制台中,在WCF中,尤其是在IIS中托管时,应该将其添加到ServiceHost或类似的程序中)。也许你应该发布这个问题的轻量级复制品,这样我们就可以看一看了。
InstallForAssembly(Assembly.LoadFile(Environment.CurrentDirectory + "\\DataContract.dll"));
ClassA instanceOfA = new ClassA();
//fill instanceOfA
var dictionary = instanceOfA.GetCustomAttributeTitles();
//do something with dictionary