从它的类名获取模型元数据 [英] Get Model Metadata from its class name
本文介绍了从它的类名获取模型元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道,我们可以使用访问模型元数据
I'm aware that we can access model meta data using
var metaData = ModelMetadataProviders.Current.GetMetadataForType(() => Model, Model.GetType());
我正在寻找的是只是用它的类名来访问模型元数据的方式,没有它的实例(可能像ModelMetadataProviders.Current.GetMetadataForType(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));
如果这一切你已经是一个字符串,再presenting类型名称,你可以加载从这个字符串类型:
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"));
这篇关于从它的类名获取模型元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文