Determine if a reflected type can be cast to another reflected type?

(implicit and/or explicit) you can use something similar to this : to.GetType(). IsAssignableFrom(from.GetType()) The Type. IsAssignableFrom() method can be used for exactly your purpose.

This would also be considerably less verbose (even if only marginally more performant) than using TypeConverters.

(implicit and/or explicit) ... you can use something similar to this : to.GetType(). IsAssignableFrom(from.GetType()); The Type. IsAssignableFrom() method can be used for exactly your purpose.

This would also be considerably less verbose (even if only marginally more performant) than using TypeConverters.

According to MSDN, IsAssignableFrom only considers equality, inheritance, interfaces, and generics, not cast operators. Msdn.microsoft. Com/en-us/library/… – Bryan Matthews Jul 1 '10 at 19:27.

It would be better to look into TypeConverter's.

There are several implementations that will dynamically generate the required proxies. For example: deftflux.net/blog/page/Duck-Typing-Proje....

Public static bool HasConversionOperator( Type from, Type to ) { Func bodyFunction = body => Expression. Convert( body, to ); ParameterExpression inp = Expression. Parameter( from, "inp" ); try { // If this succeeds then we can cast 'from' type to 'to' type using implicit coercion Expression.

Lambda( bodyFunction( inp ), inp ).Compile(); return true; } catch( InvalidOperationException ) { return false; } } This should do the trick for implicit and explicit conversions (including numeric types, classes, etc. ).

(implicit and/or explicit). What I'm trying to do is create a library that allows users to specify that a property on one type is mapped to a property on another type. Everything is fine if the two properties have matching types, but I'd like to be able to allow them to map properties where an implicit/explicit cast is available.

They would be able to say that from. IntProp will be assigned to to. LongProp (as an implicity cast exists).

But if they said that it mapped to DateTimeProp I'd be able to determine that there's no available cast and throw an exception.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions