Generic Interface<T> Instance

Jun 6, 2011 at 2:18 PM

Hi,

My Scenario,

I have an Generic Interface & Generic Class: this resides in one Project(class Library)

Interface :    interface IMethod<T> { //Method declarion }

Class      :    Class Method<T>: IMethod<T> Where T  :Method<T>{ //Method Implementation }

 My Requirement:

I Need to create an instance of Generic Interface in another project(different class library) and access those methods.

Need to have a service locator for the same

any soluiton to do thye same

Regards

-Antony

 

Jun 6, 2011 at 3:03 PM

Using a Generic Interface from Assembly A in project B is the same as using any other library, you simply add a reference to your Assembly A in whichever new assembly in project B needs that reference.

For instantiating your Generic interface via the ServiceLocator, I would recommend creating a Factory to take care of this instantiation and have that registered with your IOC container.

Jun 6, 2011 at 3:14 PM

Thanks for your reply,

could you please share some sample on how to cretae Factory for instantiating Generic Interface.?

Regards,

Antony

Jun 6, 2011 at 3:44 PM

As a quick example ...

public interface IGenericInterface<T> { }
public class GenericClass1 : IGenericInterface<int> { }
public class GenericClass2 : IGenericInterface<double> { }

public interface IGenericInterfaceFactory
{
 IGenericInterface<T> CreateGenericObject<T>();
}
public class GenericInterfaceFactory : IGenericInterfaceFactory
{
 public IGenericInterface<T> CreateGenericObject<T>()
 {
  if(typeof(T) is typeof(int))
  {
   return new GenericClass1();
  }
  else if(typeof(T) is typeof(double))
  { 
   return new GenericClass2();
  }
  
  throw new GenericTypeNotSupportedException<T>();
 }
}
This is a standard factory pattern implementation. I recommend some further reading on design paterns, specifically with relation to "Creation patterns", for example ... Factory, abstract factory method, fly-weight, et al.
Jun 6, 2011 at 4:56 PM

... and prepared for when you say "But my implementation is GenericClass<T> : IGenericInterface<T>" ...

public interface IGenericInterface<T> { }
public class GenericClass<T> : IGenericInterface<T> { }

public interface IGenericInterfaceFactory
{
 IGenericInterface<T> CreateGenericObject<T>();
}
public class GenericInterfaceFactory : IGenericInterfaceFactory
{
 public IGenericInterface<T> CreateGenericObject<T>()
 {
   return new GenericClass<T>();
 }
}

I hope this helps.

Jun 6, 2011 at 5:17 PM

MEF 2 supports open generics in the box. If you are not using MEF 4.0, another option is to look to MEF Contrib which also includes support for open generics.

Jun 8, 2011 at 11:53 AM

Thaks for your reply,

My Scenario:

The below Interface & Class Belongs to One Class Library

Genrric Interface  :    interface Iinterface<T>

Generic Class      :    ClassA<T>:  Iinterface<T> Where T  :ClassA<T>

I want to create an instance of Generic Interfcae in the below class.

Class  : SampleClass{

private Interface<SampleClass> ObjectGenericInterface;

}

SampleClass()

{

this.ObjectGenericInterface=ServiceLocator.Current.Instance<<SampleClass>>();

 

}

shows exception saying Activation was unhandled by user code..

 

but when i try to cretae an instance for noremal class its working fine

 

please suggest a solution on the same.

 

Thanks

Antony

Jun 8, 2011 at 8:20 PM

SampleClass does NOT extend Generic<T>, and you have stipulated that the parameter T must be an extension to Generic<T> so this will NEVER work.

In fact it shouldn't even compile, there is something missing from your example. Can't you paste the actual code?

I'm sorry to say that this example you have shown is just plain wrong.