I'm aware that we can access model meta data using
var metaData = ModelMetadataProviders.Current.GetMetadataForType(() => Model, Model.GetType());
What I'm looking for is a way to access model metadata just using it's class name, without it's instance (maybe something like ModelMetadataProviders.Current.GetMetadataForType(Model.GetType())). Is there an easy way to achieve that?
You don't need an instance in order to access the metadata of a type:
var metaData = ModelMetadataProviders
.Current
.GetMetadataForType(null, typeof(SomeViewModel));
And if all you have is a string representing the type name you could load the type from this string:
var metaData = ModelMetadataProviders
.Current
.GetMetadataForType(null, Type.GetType("AppName.Models.MyViewModel"));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.