【问题标题】:Get DataContract Name property获取 DataContract 名称属性
【发布时间】:2018-05-11 02:54:24
【问题描述】:

我有一个类,我应用了一个带有 name 属性的 DataContract 属性。

[DataContract(Name ="ProductInformation")]
public class ProductViewModel : BaseViewModel
{
   [DataMember]
   public string ProductName {get; set;}
}

我的所有其他 ViewModel 类也继承自 BaseViewModel 类。如何从 BaseViewModel 检索 Name 属性。

【问题讨论】:

    标签: c# wpf datacontractserializer datacontract


    【解决方案1】:

    更新

    如果我理解正确,当从基类中调用时,您需要最派生的属性。注意 typeof(T) 获取实例化类型,即派生最多的类型

    还要注意GetTypeInfo() 基本上是添加的,因为这是一个Xamarin 问题

    public static List<DataContractAttribute> GetDataContracts<T>()where T : class
    {
        return typeof(T).GetTypeInfo()
                        .GetCustomAttributes(false)
                        .OfType<DataContractAttribute>()
                        .ToList();
    }
    

    原创

    public static List<DataContractAttribute> GetDataContracts<T>()where T : class
    {
        return typeof(T).BaseType?
                        .GetCustomAttributes(false)
                        .OfType<DataContractAttribute>()
                        .ToList();
    }
    
    public static void Main()
    {
        var attribute = GetDataContracts<ProductViewModel>().FirstOrDefault();
        Console.WriteLine(attribute?.Name ?? "NoCigar");
    }
    

    【讨论】:

    • 好的,也许我应该提到我在 PCL xamarin 项目中使用它。我可以得到 BaseType,比如这个 typeof(T).GetTypeInfo().BaseType,但是我没有得到“GetCustomAttribute”
    • @LibinJoseph typeof(T).GetTypeInfo().BaseType.GetTypeInfo().GetCustomAttributes ?
    • 这部分工作得很好。但我不能使用这个 GetDataContracts().FirstOrDefault();因为代码是在baseviewmodel中实现的,这段代码需要解析所有的子类。不仅仅是 ProductViewModel。
    • @LibinJoseph 所以你想要每个级别的所有数据合约?
    • 是的,每个视图模型都会添加数据契约,并且它们也会被赋予一个名称属性。在基本视图模型中。根据当前显示的页面,我需要将 name 属性应用于数据合同。我希望这更有意义
    【解决方案2】:

    PCL BaseViewModel 代码

    public class BaseViewModel
        {
            public static Dictionary<Assembly, Type> AllAssembelyUsedInBaseViewModel = new Dictionary<Assembly, Type>();
            public static void RegisterAssemblyAndBase(Assembly assembly, Type baseType)
            {
                AllAssembelyUsedInBaseViewModel.Add(assembly, baseType);
            }
            static BaseViewModel()
            {
                RegisterAssemblyAndBase(typeof(BaseViewModel).GetTypeInfo().Assembly, typeof(BaseViewModel));
            }
    
            public static void GetDataContractNameFromAllAssembly()
            {
                List<string> dname = new List<string>();
                foreach (var item in BaseViewModel.AllAssembelyUsedInBaseViewModel)
                {
                    var assembly = item.Key;
                    var types = assembly.DefinedTypes.Where(x => x.BaseType == item.Value);
    
                    foreach (var type in types)
                    {
                        var attributes = type.GetCustomAttributes(typeof(DataContractAttribute), false);
                        var dt = attributes.FirstOrDefault() as DataContractAttribute;
                    if (dt != null)
                    {
                        dname.Add(dt.Name);
                    }
                    }
                }
            }
        }
    

    注册其他 UI 程序集

     public MainWindow()
            {
                InitializeComponent();
    
                BaseViewModel.RegisterAssemblyAndBase(typeof(MainWindow).Assembly, typeof(BaseViewModel));    
    
            }
    

    通过调用获取所有数据合约名称

    BaseViewModel.GetDataContractNameFromAllAssembly();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多