Views: 0
Replies: 0
Archived
|
,
|
|
Reply 1Hi,
I Can do it with Covarience like below sample. I want to handle common variable. public class Program { static void Main(string[] args) { IGenericFactory<IAnimal> hayvan = new GenericFactory<Cat>(); hayvan = new GenericFactory<Dog>(); IAnimal cat = hayvan2.Create(); cat.Speak(); Console.ReadLine(); } } public interface IAnimal { void Speak(); } public class Cat : IAnimal { public void Speak() { Console.WriteLine("Miyaww"); } } public class Dog : IAnimal { public void Speak() { Console.WriteLine("Hqw haww"); } } public interface IGenericFactory<out T> { T Create(); } public class GenericFactory<T> : IGenericFactory<T> where T : IAnimal, new() { public GenericFactory() { } public T Create() { return new T(); } } Best Regards. Volkan Genç, Apr 18, 2013
|