[英]c# mvc get generic type using reflection
Currently trying to find a way to get the type of a generically typed class and have been unable to find a way to do so. 当前正在尝试找到一种获取通用类型类的类型的方法,并且一直未能找到一种方法。 My current, working method looks as follows: 我当前的工作方法如下:
public class CategoryConfiguration : EntityTypeConfiguration<Category>
{
public CategoryConfiguration()
{
ToTable("Categories");
Property(c => c.Name).IsRequired().HasMaxLength(50);
}
}
and I'd like to change this method to be something like: 我想将此方法更改为:
public class CategoryConfiguration : EntityTypeConfiguration<T>
{
public CategoryConfiguration()
{
ToTable(/*get string representation of 'T' here and pluralize*/);
Property(c => c.Name).IsRequired().HasMaxLength(50);
}
}
If you would like to see a class name, use nameof(T)
. 如果要查看类名,请使用nameof(T)
。 Or, if you would like to see different name bounding with T
type, you can use an Attribute
, Description
for example (read here: https://msdn.microsoft.com/ru-ru/library/system.componentmodel.descriptionattribute(v=vs.110).aspx ), and get value of it. 或者,如果您希望看到与T
类型不同的名称边界,则可以使用Attribute
, Description
(例如,在此处阅读: https : //msdn.microsoft.com/ru-ru/library/system.componentmodel.descriptionattribute( v = vs.110).aspx ),并获取其价值。
For example: 例如:
[Description("/*name, which you would like to see*/")]
public class Category
{
/* your class realization */
}
And in CategoryConfiguration
use 并在CategoryConfiguration
使用
ToTable((typeof(T).GetCustomAttributes(typeof(DescriptionAttribute), true)
.First() as DescriptionAttribute)
.Description);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.