views:

295

answers:

5

Ok I have a generic interface

public IConfigurationValidator<T>
{
 void Validate();
}

a class that implements it:

public class SMTPServerValidator : IConfigurationValidator<string>
    {


        public void Validate(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                throw new Exception("Value cannot be null or empty");
            }
        }


    }

I now want to use reflection to create an instance of SMTPServerValidator, because I know the AssemblyQualifiedName of the type.

I was thinking to use Activator.CreateInstance and cast that to the interface... like this:

IConfigurationValidator<T> validator = (IConfigurationValidator<T>)Activator.CreateInstance(typeof(SMTPServerValidator));

I dont know what T is....how do I use reflection to create an instance of this class?

There is another class that has that I am dealing with that I left out:

public class ConfigurationSetting<T>
    {

        IConfigurationValidator<T> m_Validator;

        public ConfigurationSetting(IConfigurationValidator<T> validator)
        {
            m_Validator = validator;
        }

        public void Validate(T value)
        {
            m_Validator.Validate(value);
        }
    }

In the end I am trying to create ConfigurationSettings and need to pass in the appropriate validator based on the parameter.

A: 

In the example given, T is string. Is this what you mean?

Ben Lings
All I know going in is that I have a type name, and I know it implements the generic interface, so I want to cast it to the interface from Activator.CreateInstance(), but to cast I need to know what T is but I dont know what it is....since this could be some other class and T could be an int....or any other object type
CSharpAtl
A: 

Creating the instance of the class is easy - you've done that already with Activator.CreateInstance.

If you don't know what T is, how are you hoping to use this as an implementation of the interface?

I assume that in reality, the interface actually refers to T in the members. If it doesn't - or if you don't need those bits - then make it non-generic, or create a non-generic base interface which the base interface extends.

Jon Skeet
I have the name of the concrete type (SMTPServerValidator) and want to create an instance but want to use it through the interface.
CSharpAtl
It is possible to have a different validator class that implements the interface but with T as an int. I am willing to go a completely different direction if you have ideas....
CSharpAtl
How are you going to *use* the interface if you don't know what T is? What could you validate without knowing the type?
Jon Skeet
Yeah...I got a little higher in the logic, and what I was trying to do all fell apart....
CSharpAtl
A: 

The class you want to cast to is IConfigurationValidator<string>, as the SMTPServerValidator implements IConfigurationValidator<T>, specifying T as string

thecoop
but it could be a different class and T could be an int...but I want to still cast to the interface...so could implement IConfigurationValidator<int>
CSharpAtl
Why could it be in IConfigurationValidator<int>? SMTPServerValidator, which is what you're creating an instance of, implements IConfigurationValidator<string>
thecoop
A: 

I realized at a higher layer I do have the type....and can create it the way I was initially intending to do....

CSharpAtl
A: 

How about

Type lConfigValidatorType = typeof(IConfigurationValidator<>)
Type lSomeOtherType = typeof(T)
Type lConstructedType = lConfigValidatorType.MakeGenericType(lSomeOtherType);
var lObject = Activator.CreateInstance(lConstructedType)
Vasu Balakrishnan