“接口遍历类”: 不知是不是我的理解出错, 我的理解是 “这种说法是有问题的,表明还没有理解[接口]是个什么东东。” 按题意: interface IA{}class B:IA{} class C:IA{} class D: IA{} B b; C c; D d; IA IpublicMehtods = b= new B() ; //下面可用 IpublicMehtods 调用 b对象 中(在接口IA 声明)的方法IA IpublicMehtods = c= new C() ; //下面可用 IpublicMehtods 调用 c对象 中(在接口IA 声明)的方法IA IpublicMehtods = d= new D() ; //下面可用 IpublicMehtods 调用 d对象 中(在接口IA 声明)的方法
你看过 var vb = new B(); 吧! var 是对对象正行 猜测, 在[vb对象] 中可以调用 B类声明的所有公共方法,而对应的是 IA vb = new B(); 则是仅仅调用( IA 声明过)的 方法,这些方法的调用 改变的是 对象 (new B())内部的东西, B 是个类,接口不能 接纳 类,只能 接纳 实现了此接口的类所实例化的方法。
或者可以换一种abstract Aclass B:A class C:A class D:A通过A可以遍历出它的子类吗
1楼是个方法实际上现在我们有其他手段。微软也做了很多工作比如net4里内置的MEF机制你只需写几个特性声明就ok了 using System.ComponentModel.Composition[Export(typeof(IA))] class B:IA{}[Export(typeof(IA))] class C:IA{}[Export(typeof(IA))] class D: IA{} 而管理类public class IAFactory { [ImportMany] public IEnumerable<IA> IAs { get; set; } }则微软就会有很简单的手段,自动在程序中自动统计这些接口集合,以便你去使用有关MEF的详细资料,lz自己google一下就可以找到了
给你写了一个简单的控制台demo using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel.Composition; using System.ComponentModel.Composition.Hosting; using System.ComponentModel.Composition.Primitives;namespace ConsoleApplication1 { class Program { public static CompositionContainer Container = new CompositionContainer();
static void Main(string[] args) { mef_init();
IAFactory Iafactory = new IAFactory(); //组合IA接口 Container.ComposeParts(Iafactory); var ias = Iafactory.IAs; foreach (var IADemo in ias) { Console.WriteLine(IADemo.Export());
} Console.Read(); } /// <summary> /// 初始化MEF设置 /// </summary> private static void mef_init() { var catalog = new AggregateCatalog(); catalog.Catalogs.Add(new DirectoryCatalog(@".")); catalog.Catalogs.Add(new AssemblyCatalog(typeof(Program).Assembly)); Container = new CompositionContainer(catalog); } } interface IA { string Export(); } [Export(typeof(IA))] class B : IA { public string Export() { return "Class=B"; } } [Export(typeof(IA))] class C : IA { public string Export() { return "Class=C"; } } [Export(typeof(IA))] class D : IA { public string Export() { return "Class=" + this.GetType().FullName; } } class IAFactory {
[ImportMany(typeof(IA))] public IEnumerable<IA> IAs { get; set; } } }
我的理解是 “这种说法是有问题的,表明还没有理解[接口]是个什么东东。” 按题意:
interface IA{}class B:IA{}
class C:IA{}
class D: IA{}
B b;
C c;
D d;
IA IpublicMehtods = b= new B() ;
//下面可用 IpublicMehtods 调用 b对象 中(在接口IA 声明)的方法IA IpublicMehtods = c= new C() ;
//下面可用 IpublicMehtods 调用 c对象 中(在接口IA 声明)的方法IA IpublicMehtods = d= new D() ;
//下面可用 IpublicMehtods 调用 d对象 中(在接口IA 声明)的方法
var vb = new B();
吧! var 是对对象正行 猜测, 在[vb对象] 中可以调用 B类声明的所有公共方法,而对应的是 IA vb = new B();
则是仅仅调用( IA 声明过)的 方法,这些方法的调用 改变的是 对象 (new B())内部的东西, B 是个类,接口不能 接纳 类,只能 接纳 实现了此接口的类所实例化的方法。
或者可以换一种abstract Aclass B:A
class C:A
class D:A通过A可以遍历出它的子类吗
using System.ComponentModel.Composition[Export(typeof(IA))]
class B:IA{}[Export(typeof(IA))]
class C:IA{}[Export(typeof(IA))]
class D: IA{}
而管理类public class IAFactory
{
[ImportMany]
public IEnumerable<IA> IAs { get; set; }
}则微软就会有很简单的手段,自动在程序中自动统计这些接口集合,以便你去使用有关MEF的详细资料,lz自己google一下就可以找到了
B 是个类,接口不能 接纳 类,只能 接纳 实现了此接口的类所实例化的
方法对象。顺便说说:
接口,是个 类型(可以称为类吧,是一个没有实体,不能实例化的类,而只能是父类, 不如称这为 “影子类”好理解点。)
声接口类型的 变量:IA vb, 这个变量 vb 可以说是“指针” (说是指针不太准确,不如说是 引用 比较 准确 ) 这个“指针” 变量 只能 指向 (实现了此接口)的类所实例化的对象 的地址。
非常正确,
接口就好比 符合 (某某规格 的)CPU接口, 它可能是 源源不断被制造出来 ,也可能是 源源不断被销毁。
怎会可能被遍历呢?
只有 自已的 一堆CPU 在可以 一一遍历。
{
static void Main(string[] args)
{
IAFactory ifs=new IAFactory();
//while (ifs.IAs.GetEnumerator().MoveNext())
//{
// Console.Write()//怎么确定这个类?
//}
}
} interface IA
{
string Export();
}
[Export(typeof(IA))]
class B : IA
{
public string Export()
{
return "Class=B";
}
}
[Export(typeof(IA))]
class C : IA
{
public string Export()
{
return "Class=C";
} }
class D : IA
{
public string Export()
{
return "Class=" + this.GetType().FullName;
} }
class IAFactory
{
[ImportMany]
public IEnumerable<IA> IAs { get; set; } //public IEnumerator GetEnumerator() //该怎么去迭代
//{
//}
}
能写出个实例吗?
List<IA> objectCollection = new List<IA>();
objectCollection .Add(new B());
objectCollection .Add(new C());
objectCollection .Add(new D());//如是不要它,就去掉
objectCollection .Remove(index_intType );
//如不太会用,查一下 List<ValueType> 的用法,出可以查一下 Dictionary<KeyType,ValueType> 的用法.完成可以做到,你要监控哪个对象,就添加对哪个对象 执行Add方法,注:
注意一下,
当你对某个对象,添加监控 后(即用了Add 方法)。
这个对象 不会因 程序走出 其所在的 作用域 而对此对象 释放,(也就是说,你不用Remove 方法,就不会释放此对象占用的内存,对象一直存在。)
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition.Primitives;namespace ConsoleApplication1
{
class Program
{
public static CompositionContainer Container = new CompositionContainer();
static void Main(string[] args)
{
mef_init();
IAFactory Iafactory = new IAFactory();
//组合IA接口
Container.ComposeParts(Iafactory);
var ias = Iafactory.IAs;
foreach (var IADemo in ias)
{
Console.WriteLine(IADemo.Export());
}
Console.Read();
} /// <summary>
/// 初始化MEF设置
/// </summary>
private static void mef_init()
{
var catalog = new AggregateCatalog();
catalog.Catalogs.Add(new DirectoryCatalog(@"."));
catalog.Catalogs.Add(new AssemblyCatalog(typeof(Program).Assembly));
Container = new CompositionContainer(catalog);
} }
interface IA
{
string Export();
}
[Export(typeof(IA))]
class B : IA
{
public string Export()
{
return "Class=B";
}
}
[Export(typeof(IA))]
class C : IA
{
public string Export()
{
return "Class=C";
} }
[Export(typeof(IA))]
class D : IA
{
public string Export()
{
return "Class=" + this.GetType().FullName;
} }
class IAFactory
{
[ImportMany(typeof(IA))]
public IEnumerable<IA> IAs { get; set; }
}
}