我的方法看起来像:
public string DoObjectProperties<T>(T obj, string text)
{
}
Run Code Online (Sandbox Code Playgroud)
现在从方法中,我需要获取我传递给方法的'obj'参数的类名的字符串值.
因此,如果我传入User对象,我需要文本'user'.
获取我正在使用的属性: typeof(T).GetProperties()
我怎样才能获得课程名称?
Nic*_*ver 73
就像这样使用.Name:
typeof(T).Name
Run Code Online (Sandbox Code Playgroud)
这给出了例如"String",还有.FullName"System.String".
小智 7
obj.GetType().Name;
//Returns "ObjectName"
Run Code Online (Sandbox Code Playgroud)
Works if your method is deep into a generic method chain, as long as obj was defined at the top of the chain.
typeof(T).Name;
//Returns "T" if the method calling it also is generic.
Run Code Online (Sandbox Code Playgroud)
This only works if the calling method defined the Class of T.