Hi I try write singleton fasede pattern with generics. I have one problem, how can I call method from generic variable.
Something like this:
T1 t1 = new T1();
//call method from t1
t1.Method();
In method SingletonFasadeMethod I have compile error:
Error 1 'T1' does not contain a definition for 'Method' and no extension method 'Method' accepting a first argument of type 'T1' could be found (are you missing a using directive or an assembly reference?)
Any advace? Thank, I am beginner in C#.
All code is here:
namespace GenericSingletonFasade
{
public interface IMyInterface
{
string Method();
}
internal class ClassA : IMyInterface
{
public string Method()
{
return " Calling MethodA ";
}
}
internal class ClassB : IMyInterface
{
public string Method()
{
return " Calling MethodB ";
}
}
internal class ClassC : IMyInterface
{
public string Method()
{
return "Calling MethodC";
}
}
internal class ClassD : IMyInterface
{
public string Method()
{
return "Calling MethodD";
}
}
public class SingletonFasade<T1,T2,T3> where T1 : class,new()
where T2 : class,new()
where T3 : class,new()
{
private static T1 t1;
private static T2 t2;
private static T3 t3;
private SingletonFasade()
{
t1 = new T1();
t2 = new T2();
t3 = new T3();
}
class SingletonCreator
{
static SingletonCreator() { }
internal static readonly SingletonFasade<T1,T2,T3> uniqueInstace =
new SingletonFasade<T1,T2,T3>();
}
public static SingletonFasade<T1,T2,T3> UniqueInstace
{
get { return SingletonCreator.uniqueInstace; }
}
public string SingletonFasadeMethod()
{
//Problem is here
return t1.Method() + t2.Method() + t3.Method();
}
}
}