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