2
A
回答
3
看一看可以从反射型获得ParameterInfo
对象:
Type type = typeof(T);
ConstructorInfo[] constructors = type.GetConstructors();
// take one, for example the first:
var ctor = constructors.FirstOrDefault();
if (ctor != null)
{
ParameterInfo[] params = ctor.GetParameters();
foreach(var param in params)
{
Console.WriteLine(string.Format("Name {0}, Type {1}",
param.Name,
param.ParameterType.Name));
}
}
1
这里是搜索 - http://www.bing.com/search?q=c%23+reflection+constructor+parameters - 顶答案是ConstructorInfo与样品:
public class MyClass1
{
public MyClass1(int i){}
public static void Main()
{
try
{
Type myType = typeof(MyClass1);
Type[] types = new Type[1];
types[0] = typeof(int);
// Get the public instance constructor that takes an integer parameter.
ConstructorInfo constructorInfoObj = myType.GetConstructor(
BindingFlags.Instance | BindingFlags.Public, null,
CallingConventions.HasThis, types, null);
if(constructorInfoObj != null)
{
Console.WriteLine("The constructor of MyClass1 that is a public " +
"instance method and takes an integer as a parameter is: ");
Console.WriteLine(constructorInfoObj.ToString());
}
else
{
Console.WriteLine("The constructor of MyClass1 that is a public instance " +
"method and takes an integer as a parameter is not available.");
}
}
catch(Exception e) // stripped out the rest of excepitions...
{
Console.WriteLine("Exception: " + e.Message);
}
}
}
相关问题
- 1. 重写构造函数的参数
- 2. Unity Dependancy将运行时参数值传递给构造函数?
- 3. 如何让父类构造函数在子构造函数之前运行? (PHP)
- 4. 无参数构造函数调用2参数构造函数
- 5. 没有参数的构造函数或参数构造函数
- 6. 如何在duktape中运行构造函数时获取类名?
- 7. 如何在写入新的(日期)时将参数传递给构造函数?
- 8. 如何为参数化类编写构造函数
- 9. 如何在参数可选时获取默认构造函数
- 10. 创建具有构造函数的单例类,该构造函数接受被评估的参数运行时
- 11. swiftsuspenders构造函数参数?
- 12. 无参数构造函数
- 13. 构造函数的参数
- 14. 参数类构造函数
- 15. 从构造函数参数
- 16. LinearGradientBrush构造函数参数
- 17. ConcurrentHashMap构造函数参数?
- 18. 无参数构造函数
- 19. StructureMap构造函数参数
- 20. 如何通过参数运行方法没有在构造函数中指定
- 21. 如何在__setattr__函数中获取构造函数参数?
- 22. 在构造函数中带有2个参数的参数化运行器类
- 23. C++如何写一个构造函数?
- 24. 如何写std :: bind与构造函数
- 25. 团结集装箱传递运行构造函数的参数
- 26. 如何从主构造函数将参数传递给父构造函数?
- 27. 如何从其参数化构造函数调用构造函数?
- 28. 构造函数和无参数构造函数?
- 29. 从参数化构造函数调用默认构造函数
- 30. 构造函数调用具有参数的构造函数
看看HTTP ://stackoverflow.com/questions/6606515/name-of-the-constructor-arguments-in-c-sharp可能有帮助 – ceth 2013-03-07 08:07:55