public struct FactoryConverter<A, B>
{
public static implicit operator A(FactoryConverter<A, B> conv)
{
return Activator.CreateInstance<A>();
}
public static implicit operator B(FactoryConverter<A, B> conv)
{
return Activator.CreateInstance<B>();
}
}
public class Factory<A, B>
{
public FactoryConverter<A, B> Create()
{
return new FactoryConverter<A, B>();
}
}
使用的时候是这样的: {
public static implicit operator A(FactoryConverter<A, B> conv)
{
return Activator.CreateInstance<A>();
}
public static implicit operator B(FactoryConverter<A, B> conv)
{
return Activator.CreateInstance<B>();
}
}
public class Factory<A, B>
{
public FactoryConverter<A, B> Create()
{
return new FactoryConverter<A, B>();
}
}
Factory<Foo, Bar> f = new Factory<Foo, Bar>();
Foo foo = f.Create();
Bar bar = f.Create();
编译器会做类型安全检测.
Foo foo = f.Create();
Bar bar = f.Create();