【问题标题】:Data type of one class to other class using reflection in C#使用 C# 中的反射将一个类的数据类型转换为另一个类
【发布时间】:2014-03-25 08:44:17
【问题描述】:

我有课

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, 版本=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]],

System.Collections.Generic.List`1[[System.Web.Mvc.SelectListItem, System.Web.Mvc,版本=3.0.0.0,文化=中性, PublicKeyToken=31bf3856ad364e35]]),

但我想检索为

string,

int?,

System.Collections.Generic.List<MyClass.ViewModel>

谁能给我解决方案。

【问题讨论】:

标签: c# system.reflection


【解决方案1】:

stringint?List&lt;MyClass.ViewModel&gt; 都是 C# 结构。它们不在 .NET 本身中。

因此,您只需构建一个查找表或一个巨大的 switch 即可将类型名称转换为 .NET 关键字。

【讨论】:

    猜你喜欢
    • 2022-08-18
    • 1970-01-01
    • 2011-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多