2016-04-28 23 views
0

我有一個屬性網格顯示類的屬性一個PropertyGrid中顯示類的列表和它的屬性(我們稱之爲MyClass的)如何實現接口的C#

我想在MyClass的屬性它將包含實現接口的所有類(我們稱之爲ISomething),並將在PropertyGrid中表示爲下拉列表(當您具有枚舉時相同的行爲)

接下來,在列表中選擇其中一個類時獲取所選類的所有屬性並顯示在屬性Grid中

我做了s ome閱讀並找到了如何獲取所有類列表並創建它的實例的一些部分解決方案,但不知道如何使用此實例在屬性網格中創建類列表。

var instances = from t in Assembly.GetExecutingAssembly().GetTypes() 
      where t.GetInterfaces().Contains(typeof(ISomething)) 
        && t.GetConstructor(Type.EmptyTypes) != null 
      select Activator.CreateInstance(t) as ISomething; 

foreach (var instance in instances) 
{ 
instance.Foo(); // where Foo is a method of ISomething 
} 

有什麼建議嗎?

+0

此代碼與屬性網格無關。你在物業網格區域嘗試過什麼? –

+0

我沒有實現它到網格,你是對的,這將無法正常工作 –

回答

0

使用Ninject

var kernel = new StandardKernel(); 
kernel.Bind<ISomething>().To<Something>(); 
kernel.Bind<ISomething>().To<Something2>(); 

var instances = kernal.GetAll<ISomething>(); 
foreach (var instance in instances) 
{ 
    instance.Foo(); 
} 
0

下面的代碼將返回對象的List並且每個對象將有類名和屬性名的List

var classesWithProperties = 
    Assembly.GetExecutingAssembly() 
      .GetTypes() 
      .Where(t => t.GetInterfaces().Contains(typeof(ISomething)) && t.IsClass) 
      .Select(c => new { ClassName = c.FullName, Properties = c.GetProperties().Select(p => p.Name)}) 
      .ToList(); 

然後你就可以通過這個迭代收藏並按照你的意願展示它。

+0

所以在我通過classesWithProperties中的每個對象之後,我將如何顯示類集合列表以及每個類的屬性和屬性值。它不返回屬性列表。另外我假設在你的代碼Ilist是在我的情況下isomething –

+0

正確。剛剛編輯。 – MaKCbIMKo

+0

這裏我做了什麼到目前爲止Dictionary classObject = new Dictionary ();在我調用我的代碼我做foreach(var類在classesWithProperties) { classObject.Add(field.ClassName,field.Properties as ISomething); }當我調用propertygrid調用屬性與指令名單我得到名稱正確的另一邊我得到ISomthing-> Value->鍵和價值提交沒有任何值 –