Reputation: 360
I have two different bundles like WebBundle and MobileBundle and I have one singleton generic class that has type of any bundle(added the generic singleton code below).
public class GenericSingleton<T> where T : class, new()
{
private static readonly object padlock = new object();
private GenericSingleton() { }
private static T instance = null;
public static T Instance {
get
{
lock (padlock)
{
if (instance == null)
{
instance = new T();
}
return instance;
}
}
}
}
When I execute the below line,
var web = GenericSingleton<WebBundle>.Instance;
I am getting instance of webbundle class in web variable and intellisense gives me all the available method in WebBundle.
Now my requirement is to assign this var web method variable to a class level property or variable and that should be dynamic for any kind of bundle.
Please help to resolve this issue
Upvotes: 1
Views: 80
Reputation: 4428
You need to extract common interface or abstract class (or ordinary class, just some common part) that those objects share. They have corresponding name which suggests they are similar. Perhaps they have the same or similar public methods (not all perhaps but some), public properties or events. You need to refactor your code to extract those common things to one entity and make T implement it in generic class:
public class GenericSingleton<T> where T : ICommonInterface
Upvotes: 1