how to determine if typ conversion exists?

T

Thomas

Hello all,

How do I determine if conversion (implicit or explicit) between integral
types exists?
I tried using Type.IsAssignableFrom() method but with no satisfactory
results.
Example below.
I want to determine in code if certain assignments, like: float f = 0; int i
= 0; f=i;
are allowed or not, and which require explicit conversion (i=(int)f;).

Thanks for any hints.

Thomas

Type t1 = typeof(int);
Type t2 = typeof(float);
Type t3 = typeof(bool);
Console.WriteLine(t2.IsAssignableFrom(t1));
Console.WriteLine(t1.IsAssignableFrom(t3));
 
P

Peter Duniho

Thomas said:
Hello all,

How do I determine if conversion (implicit or explicit) between integral
types exists?

Read the C# specification.
I tried using Type.IsAssignableFrom() method but with no satisfactory
results.

Correct. That's strictly based on casting allowed by the .NET type
system and doesn't take into account conversions.
Example below.
I want to determine in code if certain assignments, like: float f = 0;
int i = 0; f=i;
are allowed or not, and which require explicit conversion (i=(int)f;).

To the extent that literals can be be converted to specific types, or
that variables of one built-in type can be converted to another built-in
type, the C# specification describes this in detail. There should be no
need to make the determination "in code".

It would help if you could elaborate on that particular aspect of your
question, so that an answer that helps you achieve the goal, rather than
some specific implementation you've already chosen, can be provided.

Pete
 
T

Thomas

Peter,

I am talking about finding out if type conversion exists *in code*. I am
really not a newbie.
int and float were just examples - very well it could be any, *a priori
unknown* custom integral type (struct).
The question is how through reflection determine if type implements certain
conversion and how (implicit/explicit).

Thanks,

Thomas
 
P

Peter Duniho

Thomas said:
Peter,

I am talking about finding out if type conversion exists *in code*.

You'll have to define what you mean by "*in code*" more specifically.
Repeating the phrase with more emphasis doesn't add anything.
int and float were just examples - very well it could be any, *a priori
unknown* custom integral type (struct).

I'm not sure why you're using the word "integral" to describe structs in
general, but it's not how I'd use the word. To me, "integral" means
"built-in", and I believe that would be a commonly understood
definition. A user-defined type is specifically not "integral", because
it's not an integral part of the language.
The question is how through reflection determine if type implements
certain conversion and how (implicit/explicit).

User-defined conversions are simply static operator methods in the
class. So you can use reflection to look for those methods. They are
named "op_Implicit" and "op_Explicit" for implicit and explicit
conversions, respectively (the parameter and return types determine
which method of those names is applicable in context).

Judging from your first post, in which you specifically state you are
not interested in explicit conversions, you'll want to limit your search
to those that are implicit.

If that doesn't answer your question, perhaps you could post a code
example. That is, a type for which you'd want to know the conversion,
and a code example showing how you'd use the hypothetical solution
you're asking for.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top