2011-05-22 94 views
1
private static object CreateObject(Type itemType) 
{ 
    try 
    { 
     if (itemType.FullName == "System.Uri") 
     { 
      ???? 
     } 

     return Activator.CreateInstance(itemType); 
    } 
    catch (Exception) 
    { 
     return itemType.GetConstructor(new Type[] { }).Invoke(new object[] { }); 
    } 
} 

回答

2

由於Uri是不可變的(非常類似於字符串),所以創建沒有值的對象是沒有意義的。當您知道Uri時,請使用常規構造函數,例如new Uri(string) - 或使用TryCreate

順便說一句 - 你也可以測試:if(itemType == typeof(Uri))