7

So I have a Generic class (it's mostly a container class) with implicit casting, like this:

public class Container<T>  
{  
        public T Value { get; set; }

        public static implicit operator T(Container<T> t)
        {
            return t.Value;
        }

        public static implicit operator Container<T>(T t)
        {
            return new Container<T>() { Value = t };
        }
} 

So in runtime I would like to cast an instance of Container<int> to int using reflection but cannot seem to find a way, I've tried the "Cast" method invoking mentioned in a couple of places but I'm getting an Specified cast is not valid. exception.

Any help will be appreciated.

4
  • 1
    So what do you know at compile time and what do you know at execution time? Could you give us the calling code? Commented Aug 15, 2011 at 13:28
  • Are you trying to "Cast" Container to int, or Container.Value? Commented Aug 15, 2011 at 13:28
  • 1
    Why don't you just call Container.Value? Commented Aug 15, 2011 at 13:30
  • I will not necessarily know that the instance of a generic variable has a 'Value' property, this was just a simplified example, essentially I want to (attempt to) cast the container variable to the type that it contains, e.g. cast Tuple<int> to int, assuming Tuple<int> actually has a implicit cast to it's contained type, or like my Container, cast a Container<Customer> to Customer. Commented Aug 16, 2011 at 5:10

3 Answers 3

8

There's almost never a good reason to do this unless the type in question is internal to an assembly that you cannot modify.

But if it came to that, I would personally prefer the much cleaner-looking dynamic solution (as mentioned by jbtule) to reflection.

But since you asked for a solution with reflection (perhaps you are on .NET 3.5 or earlier?), you can do:

object obj = new Container<int>();

var type = obj.GetType();
var conversionMethod = type.GetMethod("op_Implicit", new[] { type });
int value = (int)conversionMethod.Invoke(null, new[] { obj });
Sign up to request clarification or add additional context in comments.

Comments

1

By using the dlr, accessible by the open source ImpromptuInterface in nuget, you can dynamically call an implicit or explicit cast.

int intInstance =Impromptu.InvokeConvert(containerInstance, typeof(int));

although this example is rather trival and can be accomplished via

int intInstance = (dynamic) containerInstnace;

as well. but if you don't know int at compile time Impromptu is the way to go.

Comments

0

Writing implicit operators allow you to make the casts implicitly. In other words, this is perfectly legal:

int i = new Container<int>() { Value = 2 };
if (i == 2) 
{
    // This will be executed
}

If you only have a Container<object> then that won't work, but in that case your code should probably be refactored anyway, since you're essentially ignoring the generic parameter you have.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.