How to get metadata custom attributes?

后端 未结 5 534
南方客
南方客 2021-01-05 08:23

I have a class that defines data annotations at class level. The meta data class has custom attributes associated with it, along with the usual DisplayName,

5条回答
  •  遥遥无期
    2021-01-05 08:56

    Based on the other answers, i'm successed to get the DisplayName attribute from MetadataType class in this way:

    var metadataType = type.GetCustomAttributes(typeof(MetadataTypeAttribute), true)
                .OfType().FirstOrDefault();
    
            var metaData = (metadataType != null)
                    ? ModelMetadataProviders.Current.GetMetadataForType(null, metadataType.MetadataClassType)
                    : ModelMetadataProviders.Current.GetMetadataForType(null, type);
    
            List propertyList = metaData.Properties.
                Select(x => x.DisplayName).ToList();
    

提交回复
热议问题