[英]Data type of one class to other class using reflection in C#
我有一堂課
namespace MyNamespace1
{
class MyClass
{
public string MyName { get; set; }
public int? MaxHeaderLength { get; set; }
public System.Collections.Generic.List<MyClass.ViewModel> Preferences { get; set; }
}
}
namespace MyNamespace2
{
internal class myClass1
{
public static void Main(string[] args)
{
var properties = loading assembly of the MyClass of MyNamespace1
var prop = properties.GetProperties();
foreach (var propertyInfo in prop)
{
Console.WriteLine(propertyInfo.PropertyType.FullName);
}
}
}
}
當我想在運行時使用反射讀取屬性時
我正在獲取類型
System.String,
System.Nullable`1 [[System.Int32,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089]],
System.Collections.Generic.List`1 [[System.Web.Mvc.SelectListItem,System.Web.Mvc,Version = 3.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35]]),
但我想檢索為
string,
int?,
System.Collections.Generic.List<MyClass.ViewModel>
誰能給我解決方案。
string
, int?
和List<MyClass.ViewModel>
都是C#構造。 它們不在.NET本身中。
因此,您只需要構建一個查找表或一個巨大的switch
即可將類型名稱轉換為.NET關鍵字。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.