Reflection: information about constructors in C#
Forums:
//SampleLib.MacBook - full class name
//SampleLib - assembly name
var macType = Type.GetType("SampleLib.MacBook, SampleLib");
var cList = macType.GetConstructors();
foreach (var c in cList) {
string info = GetModifiers(c) +
macType.Name + "(" +
GetParameters(c.GetParameters()) + ")";
Console.WriteLine(info);
}
string GetModifiers(ConstructorInfo c) {
string s = "";
if (c.IsPublic) s += "public ";
if (c.IsAbstract) s += "abstruct ";
if (c.IsPrivate) s += "private ";
if (c.IsStatic) s += "static ";
return s;
}
string GetParameters(ParameterInfo[] list) {
string s = "";
for (int i = 0; i < list.Count(); i++) {
if (i > 0) s += ", ";
s += list[i].ParameterType.Name + " param" + (i + 1);
}
return s;
}
- Log in to post comments