Reputation: 175
I try to get Full namespace path of given class name example like input class name ="ABC"
a class ABC located on A.B namespace
i need full path like A.B.ABC
my input type pass in string like name of class not type
Type t= Type.GetType("A.B.ABC"); working
Type t= Type.GetType("ABC"); not working
how to find A.B.ABC on ABC
Code :
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();
}
}
Upvotes: 0
Views: 7112
Reputation: 31097
You could use Linq to scan your assembly for classes with the same name:
ClassType = Assembly.GetAssembly(typeof(UcDataGridView)).GetTypes().FirstOrDefault(t => t.Name == _ClassFullName);
You have to be certain that UcDataGridView
does not appear more than once in the same assembly.
Upvotes: 0
Reputation: 21
string fullPathName= typeof(className).AssemblyQualifiedName;
Upvotes: 0