【发布时间】:2013-07-16 16:52:11
【问题描述】:
我尝试获取给定类名的完整命名空间路径 像这样的例子 输入类名="ABC"
位于 A.B 命名空间中的 ABC 类
我需要像 A.B.ABC 这样的完整路径
我的输入类型传入字符串,如类名而不是类型
Type t= Type.GetType("A.B.ABC");工作
类型 t= Type.GetType("ABC");不工作
如何在 ABC 上找到 A.B.ABC
代码:
public partial class UcDataGridView : DataGridView
{
private string _ClassFullName = "UcDataGridView";
[Browsable(true), Category("Misc")]
public string ClassFullName
{
get
{ return _ClassFullName; }
set
{
_ClassFullName = value;
if (!string.IsNullOrEmpty(_ClassFullName))
{
ClassType = Type.GetType(_ClassFullName);
if (ClassType != null)
{
if (ClassType.IsClass)
{
PropertyInfo[] props = ClassType.GetProperties();
foreach (var item in props)
{
var txtCol = new DataGridViewTextBoxColumn();
txtCol.Name = "C" + item.Name;
txtCol.HeaderText = item.Name;
txtCol.DataPropertyName = item.Name;
txtCol.ReadOnly = true;
this.Columns.Add(txtCol);
}
}
else
this.Columns.Clear();
}
else
this.Columns.Clear();
}
else
this.Columns.Clear();
Invalidate();
}
}
private Type ClassType { get; set; }
public UcDataGridView()
{
InitializeComponent();
}
}
【问题讨论】:
-
typeof(ABC).ToString()有什么问题? -
ABC.cs是文件名,不是类名。 -
typeof(ABC).FullName会更明确,并且不依赖于ToString()的行为。 -
.cs和这个有什么关系?您的代码在什么上下文中运行,您是在读取.cs文件还是文件已编译并且ABC类存在于您的程序集中或为您的程序集所知? -
.cs 是文件名的扩展名
标签: c#