我製作了項目Animal並將其設置爲庫。當然,在動物項目中,我會添加更多課程。在這個項目中,我現在有兩個類:狗和方法。爲字典中的對象創建新實例
//Class Method
public class Method : Attribute{
public string Name { get; set; }
public Method(){
Name = "";
}
}
//Class Dog
[Method(Name = "dog")]
public class Dog
{
public int NumberOfLegs { get; set; }
public string Breed { get; set; }
public Dog() { }
}
然後,我在我的解決方案參考創建第二個項目,這是我的主類:
//version 1
class Program{
public static Dictionary<String, Type> animals;
static void Main(string[] args){
Assembly assembly = Assembly.GetEntryAssembly();
Type[] typ = assembly.GetTypes();
foreach (Type t in typ){
animals.Add(t.Name, t);
}
}
}
我想實現「狗,狗」,「貓,貓」,「老鼠,老鼠」 。其中,「狗」是name屬性和「狗」是一種感謝,我將能夠使像
Activator.CreateInstance(animals[nameAnimal]);
但現在我做錯了什麼,我的方法不工作,我無法找到工作解決我的問題.net核心。我只想從屬性方法的類中獲得對象。如何實現它?
編輯。 我的程序沒有看到圖書館「狗」,搜索例如參考,Ref.Program等
*什麼*不工作? – InBetween
您是否使用Visual Studio? –
@JonathanWillcock是的,2017 – nju