如果我在C#中的字符串中有类型名称,如何获取对象的公共属性?

如果我在C#中的字符串中有类型名称,如何获取对象的公共属性?,c#,reflection,t4,asp.net-mvc-scaffolding,C#,Reflection,T4,Asp.net Mvc Scaffolding,我有一长串C#中的对象,我想通过MvcScaffolding为它们编写一系列“单元测试存根” 我希望能够加载对象的元数据并通过对象的公共属性循环 但是,在上下文中,我不想(在某些情况下不能)创建对象的实际副本 如果我只有字符串中的类型名称,在C#中是否有办法读取对象的公共属性 我正在尝试在T4模板中加载属性 例如,我正在尝试: <#@ Template Language="C#" HostSpecific="True" Inherits="DynamicTransform" #> &

我有一长串C#中的对象,我想通过MvcScaffolding为它们编写一系列“单元测试存根”

我希望能够加载对象的元数据并通过对象的公共属性循环

但是,在上下文中,我不想(在某些情况下不能)创建对象的实际副本

如果我只有字符串中的类型名称,在C#中是否有办法读取对象的公共属性

我正在尝试在T4模板中加载属性

例如,我正在尝试:

<#@ Template Language="C#" HostSpecific="True" Inherits="DynamicTransform" #>
<#@ assembly name="C:\www\myproject\Web\bin\MyCustomAssembly.dll" #>
<#@ Output Extension="cs" #>
<#@ import namespace="MyCustomAssembly" #>

[TestClass]
public class <#= Model.PluginName #>ServiceTest
{
    // Example model value from scaffolder script: <#= Model.ExampleValue #>
    // 
<# 
    Type foo = Type.GetType("MyCustomAssembly."+Model.PluginName + "Service");
    if (foo == null) {
#>
// <#= "NULL" #>
<# } else {  
        // Output one generic UnitTest per Property in the object type
   } #>

}

[测试类]
公共类服务测试
{
//脚手架脚本中的模型值示例:
// 
// 
}
}

如果你有类型,那么你可以使用方法。

如果你有类型,那么你可以使用方法。

如果你有类型,那么你可以使用方法。

如果你有类型,那么你可以使用方法。

像这样:

foreach (PropertyInfo p in Type.GetType(typeName).GetProperties())
{
    Console.WriteLine(p.Name);
}
像这样:

foreach (PropertyInfo p in Type.GetType(typeName).GetProperties())
{
    Console.WriteLine(p.Name);
}
像这样:

foreach (PropertyInfo p in Type.GetType(typeName).GetProperties())
{
    Console.WriteLine(p.Name);
}
像这样:

foreach (PropertyInfo p in Type.GetType(typeName).GetProperties())
{
    Console.WriteLine(p.Name);
}
Type.GetType(typeName).Properties
Lookup c#reflection in Google.请参阅
Type.GetType(typeName).Properties
Lookup c#reflection in Google.请参阅
Type.GetType(typeName).Properties
Lookup c#reflection in Google.请参阅