簡體   English   中英

在C#中使用反射將一類的數據類型轉換為另一類

[英]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>

誰能給我解決方案。

stringint? List<MyClass.ViewModel>都是C#構造。 它們不在.NET本身中。

因此,您只需要構建一個查找表或一個巨大的switch即可將類型名稱轉換為.NET關鍵字。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM