开发者

Method that takes type of object to create and type of interface it should return - is it possible?

Imagine C# code like this

ISomeInterface someVar = creator.Create(typeof(SomeClass), 
                                        typeof(ISomeInterface));

What should happen? Method creator.Create takes two parameters: first is a type of an object to be created, second is a type that method should cast newly created object to and return as that type.

So, like in example code: we want use interface we know class implements so we ask for creation of object and specify that returned type should be that interface we are interested in.

Is is even possible to implement it somehow? Generics, reflection, dynamic method creation based on some complicated generics declaration? something more sophisticated? I'm open for suggestions.

Of course we are not taking into consideration simplest solution that is explicitly casting returned value to ISomeInterface. Method should return created object as interface we asked for.

If you wonder what do I need it for I'm trying to complicate my working code in futile pursuit of some clever way of simplifying my class factory :)

EDIT:

Current generic method Create:

public T Create<T>(Type type)
{
   return (T)Activator.CreateInstance(type);
}

But this is not what I want which is provide two types and basically create object of first but return as second. It is not a case that I have Create method for different interfaces implemented in my codebase.

Further example of usage:

  IFoo f = Create(typeof(Foo), typeof(IFoo));
  IBar b = Create(typeof(Bar), typeof(IBar));

So, as you can see method returns different interfaces each time according to type provided in second parameter. And there is no way to use typ开发者_如何学编程e constrains or I don't see how to do it.


Off the top of my head, you could do something like this:

public I Create<T, I>() where T : I {
    if(typeof(I).IsInterface) {
        return Activator.CreateInstance<T>();
    }
    /* Handle non-interface I calls with exception, etc */
}


If you want your Create() method to return ISomeInterface without having to explicitly cast, then you obviously need to make it Create<ISomeInterface>().

To dynamically construct and instance of passed type you obviously need to use reflection, though Activator would make it easier.


You could use generic type constraints:

public T Create<T,U,V> (U var1, V var2) 
where T: ISomeInterface 
where U: SomeClass
where V: ISomeInterface
{ 
 //Some code
}


This does not seem like a very safe thing to do.

public TResult CreateToType<TType,TResult>() where TType : new() where TResult : class
{
    return new TType() as TResult;
}

Edit - hmm this actually won't do what you want, as your trying to cast to an interface. Woops.


I think you are referring to Duck Typing, if it looks like a duck then it is a duck!

You should check out a few of these links

Wikipedia Duck Typing
Duck Typing Explained

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜