I need to merge these 3 properties in just a general one.
How can I do it??

public class BonusImageHandler
{
    private static IStorageProvider _BAstorageProvider; // 1        
    private static IStorageProvider _BAWP8storageProvider; // 2     
    private static IStorageProvider _M4MstorageProvider; // 3       

    private static IStorageProvider BAStorageProvider // for 1
    {
        get
        {    var storageProviderType = Type.GetType("BAStorageProviderTypeName");
            _BAstorageProvider = (IStorageProvider)Activator.CreateInstance(storageProviderType);
            return _BAstorageProvider;
        }
    }
    ////////////////////////////////////////////////////
    private static IStorageProvider BAWP8StorageProvider // for 2
    {
        get
        {    var storageProviderType = Type.GetType("BAWP8StorageProviderTypeName");
            _BAWP8storageProvider = (IStorageProvider)Activator.CreateInstance(storageProviderType);   
            return _BAWP8storageProvider;
        }
    }
    ////////////////////////////////////////////////////////
    private static IStorageProvider M4MStorageProvider // for 3
    {
        get
        {    var storageProviderType = Type.GetType("M4MStorageProviderTypeName");
            _M4MstorageProvider= (IStorageProvider)Activator.CreateInstance(storageProviderType);                   
            return _M4MstorageProvider;
        }
    }
}

Recommended Answers

All 4 Replies

How do you want this general property to work? Will users of this class be providing the storage type name?

commented: yes, exactly. +0

And why are you going through the reflection API, why not just instantiate the classes directly?

You could make use of generics.

    class MyInterfaceFactory
    {
        public static T GetClass<T>() where T : myInterface
        {
            myInterface temp = null;

            if (typeof(T) == typeof(ClassOne)) temp = new ClassOne();
            else if (typeof(T) == typeof(ClassTwo)) temp = new ClassTwo();
            else if (typeof(T) == typeof(ClassThree)) temp = new ClassThree();

            return (T)temp;
        }
    }

This is a pretty simple example, you should be able to implement it with your interface and classes fairly easily I think.

commented: what does myInterface contain?? what is the implementation of ClassOne, for example?? +0

in this example myInterface is just a blank interface. Every class that gets returned implements the interface, just as in your code all 3 of your storage providers must implement IStorageProvider. In the code I provided you should be able to swap out my "myInterface" with "IStorageProvider", and swap out your classes for the example classes I used. It should work fine as long as all of your classes implement IStorageProvider, which it looks like they do from your code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.