简体   繁体   English

.Net Core中的.NET Framework ModelMetadataProvider相当于什么?

[英]What is the equivalent of .NET Framework ModelMetadataProvider in .Net Core?

for get property DisplayName attribute value, ModelMetadataProvider used at .NET Framework versions.对于获取属性DisplayName属性值, ModelMetadataProvider用于 .NET 框架版本。

ModelMetadataProviders.Current.GetMetadataForProperty(null, ClassType, PropertyName).DisplayName;

and I tried get DisplayName in .NET 6:我尝试在 .NET 6 中获取DisplayName

class SampleClass{
   [Display(Name = "FirstName", ResourceType = typeof(MyResource))]
   public string Name{ get; set; }
}

Type ClassType = typeof(SampleClass);
string PropertyName = "Name";
string displayName = TypeDescriptor.GetProperties(ClassType)
                            .Cast<PropertyDescriptor>()
                            .ToDictionary(p => p.Name, p => p.DisplayName)
                            .FirstOrDefault(p => p.Key == PropertyName).ToString(); 

The DisplayName value will be received, but the output value does not change when the CultureInfo is changed!将收到DisplayName值,但更改 CultureInfo 时 output 值不会更改!

using the following code, DisplayName value (which has a resource) can be accessed without using ModelMetadataProvider and dependency injection, and it also works by changing the language.使用下面的代码,可以在不使用ModelMetadataProvider和依赖注入的情况下访问DisplayName值(具有资源),并且它也可以通过更改语言来工作。

class SampleClass{
   [Display(Name = "FirstName", ResourceType = typeof(MyResource))]
   public string Name{ get; set; }
}

Type ClassType = typeof(SampleClass);
string PropertyName = "Name";
string displayName = "";

MemberInfo property = ModelType.GetProperty(Property);
displayName = property.GetCustomAttribute<DisplayAttribute>()?.GetName();
            

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否有等效于 .NET Core 的 IControllerActivator 的 .NET Framework? - Is there a .NET Framework equivalent to .NET Core's IControllerActivator? KerberosRequestorSecurityToken的.Net核心等效项是什么? - What is the .Net core equivalent of KerberosRequestorSecurityToken? 在我的.Net框架应用程序中,“startup.cs”(.Net Core)的等价物是什么? - what is the equivalent of “startup.cs”(.Net Core) in my .Net framework application? C#.net核心中的等效examplematcher - What equivalent examplematcher in C# .net core .NET Core 3.1 中实体的等价物是什么? - What can be the equivalent of entity in .NET Core 3.1? 什么是.NET Core等效于Encoding.GetEncodings() - What is .NET Core equivalent of Encoding.GetEncodings() .NET Core中的Assembly.GetEntryAssembly()相当于什么? - What is the equivalent of Assembly.GetEntryAssembly() in .NET Core? .NET Core 中 [Serializable] 的等价物是什么? (转换项目) - What is the equivalent of [Serializable] in .NET Core ? (Conversion Projects) 与HttpRequestMessage等效的ASP.NET Core是什么? - What is the ASP.NET Core equivalent to HttpRequestMessage? 什么是CultureInfo.GetCultures是.NET Core - What is the equivalent of CultureInfo.GetCultures is .NET Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM