Setting a property by reflection, with implicit conversion

G

Guest

Given the classes:

class Class
{
public static implicit operator int(Class c)
{
return 0;
}
}
class Holder
{
public int Property
{
set {}
}
}

I can write the code:

Holder h = new Holder();
h.Property = new Class();

How can I do the same thing using reflection, without making any changes to
the classes used?

I have tried:

System.Reflection.PropertyInfo pi =
Type.GetType("Holder").GetProperty("Property");
pi.SetValue(h, c, System.Reflection.BindingFlags.SetProperty,
null, new object[] {}, null);

An exception gets thrown:

An unhandled exception of type 'System.ArgumentException'
occurred in mscorlib.dll

Additional information: Cannot widen from target type to
primitive type.
 
S

Shardool Karnik

try

System.Reflection.PropertyInfo pi =
Type.GetType("Holder").GetProperty("Property");

pi.SetValue(h, "VALUE", null);
 
G

Guest

I've tried that too, but got the same result. :)

Given a System.Type, I need some way to convert any object to that type at
runtime, while honouring the implicit conversions the real type of the object
is capable of. I know I could implement it by searching typeof(object) and
typeof(target) for suitable implicit conversions, but I'm hoping there's a
method already implemented.

Shardool Karnik said:
try

System.Reflection.PropertyInfo pi =
Type.GetType("Holder").GetProperty("Property");

pi.SetValue(h, "VALUE", null);


Aaron Queenan said:
Given the classes:

class Class
{
public static implicit operator int(Class c)
{
return 0;
}
}
class Holder
{
public int Property
{
set {}
}
}

I can write the code:

Holder h = new Holder();
h.Property = new Class();

How can I do the same thing using reflection, without making any changes to
the classes used?

I have tried:

System.Reflection.PropertyInfo pi =
Type.GetType("Holder").GetProperty("Property");
pi.SetValue(h, c, System.Reflection.BindingFlags.SetProperty,
null, new object[] {}, null);

An exception gets thrown:

An unhandled exception of type 'System.ArgumentException'
occurred in mscorlib.dll

Additional information: Cannot widen from target type to
primitive type.
 
S

Shardool Karnik

ok now i am confused ... i use reflection all the time on my classes .... I
can help if you are a little clear ....

from what I understand .. you have a user defined type .. and u want to
populate a property value ... is that correct ??
but you want to use Type.GetType instead of typeof ???



Aaron Queenan said:
I've tried that too, but got the same result. :)

Given a System.Type, I need some way to convert any object to that type at
runtime, while honouring the implicit conversions the real type of the object
is capable of. I know I could implement it by searching typeof(object) and
typeof(target) for suitable implicit conversions, but I'm hoping there's a
method already implemented.

Shardool Karnik said:
try

System.Reflection.PropertyInfo pi =
Type.GetType("Holder").GetProperty("Property");

pi.SetValue(h, "VALUE", null);


Given the classes:

class Class
{
public static implicit operator int(Class c)
{
return 0;
}
}
class Holder
{
public int Property
{
set {}
}
}

I can write the code:

Holder h = new Holder();
h.Property = new Class();

How can I do the same thing using reflection, without making any
changes
to
the classes used?

I have tried:

System.Reflection.PropertyInfo pi =
Type.GetType("Holder").GetProperty("Property");
pi.SetValue(h, c, System.Reflection.BindingFlags.SetProperty,
null, new object[] {}, null);

An exception gets thrown:

An unhandled exception of type 'System.ArgumentException'
occurred in mscorlib.dll

Additional information: Cannot widen from target type to
primitive type.
 
J

Jay B. Harlow [MVP - Outlook]

Aaron,
Have you looked at Convert.ChangeType & the IConvertible interface?

Class would implement IConvertible providing ToInt32 in this case, it may
need to provide Convert.ToType in other cases.

Class c = ...;
Holder h = ...;

| > System.Reflection.PropertyInfo pi =
h.GetType().GetProperty("Property");
| >
Object v = Convert.ChangeType(c, pi.PropertyType);
| > pi.SetValue(h, v , null);


Another option may be System.ComponentModel & its TypeConverter framework,
however I have not done a lot with TypeConverters...

Hope this helps
Jay

| I've tried that too, but got the same result. :)
|
| Given a System.Type, I need some way to convert any object to that type at
| runtime, while honouring the implicit conversions the real type of the
object
| is capable of. I know I could implement it by searching typeof(object) and
| typeof(target) for suitable implicit conversions, but I'm hoping there's a
| method already implemented.
|
| "Shardool Karnik" wrote:
| > try
| >
| > System.Reflection.PropertyInfo pi =
| > Type.GetType("Holder").GetProperty("Property");
| >
| > pi.SetValue(h, "VALUE", null);
| >
| >
message
| > | > > Given the classes:
| > >
| > > class Class
| > > {
| > > public static implicit operator int(Class c)
| > > {
| > > return 0;
| > > }
| > > }
| > > class Holder
| > > {
| > > public int Property
| > > {
| > > set {}
| > > }
| > > }
| > >
| > > I can write the code:
| > >
| > > Holder h = new Holder();
| > > h.Property = new Class();
| > >
| > > How can I do the same thing using reflection, without making any
changes
| > to
| > > the classes used?
| > >
| > > I have tried:
| > >
| > > System.Reflection.PropertyInfo pi =
| > > Type.GetType("Holder").GetProperty("Property");
| > > pi.SetValue(h, c, System.Reflection.BindingFlags.SetProperty,
| > > null, new object[] {}, null);
| > >
| > > An exception gets thrown:
| > >
| > > An unhandled exception of type 'System.ArgumentException'
| > > occurred in mscorlib.dll
| > >
| > > Additional information: Cannot widen from target type to
| > > primitive type.
| > >
| >
| >
| >
 
G

Guest

Both of the types in my example will be created using reflection, so I will
only have a "reference to object" for each of them, and will have to find out
the type of the property at runtime as well.

In my example, "Class" has an implicit conversion to int, and
"Holder.Property" is of type int. As a result, I can assign an object of type
"Class" to "Holder.Property" at compile time because the compiler knows how
to implicitly convert to the correct type.

I want to convert from "Class" to int at runtime where the types are not
known at compile time. For example:

Holder h = new Holder();
h.Property = (object)new Class();

This won't compile because "object" can't convert to int. If I just had an
object reference, and didn't know the type of the object at compile time, how
could I call the appropriate implicit conversions to assign the property?

Shardool Karnik said:
ok now i am confused ... i use reflection all the time on my classes .... I
can help if you are a little clear ....

from what I understand .. you have a user defined type .. and u want to
populate a property value ... is that correct ??
but you want to use Type.GetType instead of typeof ???



Aaron Queenan said:
I've tried that too, but got the same result. :)

Given a System.Type, I need some way to convert any object to that type at
runtime, while honouring the implicit conversions the real type of the object
is capable of. I know I could implement it by searching typeof(object) and
typeof(target) for suitable implicit conversions, but I'm hoping there's a
method already implemented.

Shardool Karnik said:
try

System.Reflection.PropertyInfo pi =
Type.GetType("Holder").GetProperty("Property");

pi.SetValue(h, "VALUE", null);


Given the classes:

class Class
{
public static implicit operator int(Class c)
{
return 0;
}
}
class Holder
{
public int Property
{
set {}
}
}

I can write the code:

Holder h = new Holder();
h.Property = new Class();

How can I do the same thing using reflection, without making any changes
to
the classes used?

I have tried:

System.Reflection.PropertyInfo pi =
Type.GetType("Holder").GetProperty("Property");
pi.SetValue(h, c, System.Reflection.BindingFlags.SetProperty,
null, new object[] {}, null);

An exception gets thrown:

An unhandled exception of type 'System.ArgumentException'
occurred in mscorlib.dll

Additional information: Cannot widen from target type to
primitive type.
 
G

Guest

Convert.ChangeType seems to need the IConvertible interface on the objects,
and I can't (and shouldn't need to) change the objects.

The compiler knows how to do the implicit conversions, and I could implement
the compiler's logic, but it would be great if there was an easy way to do
the implicit conversion automatically.

TypeConverter throws the following exception:

An unhandled exception of type 'System.NotSupportedException'
occurred in system.dll

Additional information: 'TypeConverter' is unable to convert
'Class' to 'System.Int32'.

Jay B. Harlow said:
Aaron,
Have you looked at Convert.ChangeType & the IConvertible interface?

Class would implement IConvertible providing ToInt32 in this case, it may
need to provide Convert.ToType in other cases.

Class c = ...;
Holder h = ...;

| > System.Reflection.PropertyInfo pi =
h.GetType().GetProperty("Property");
| >
Object v = Convert.ChangeType(c, pi.PropertyType);
| > pi.SetValue(h, v , null);


Another option may be System.ComponentModel & its TypeConverter framework,
however I have not done a lot with TypeConverters...

Hope this helps
Jay

| I've tried that too, but got the same result. :)
|
| Given a System.Type, I need some way to convert any object to that type at
| runtime, while honouring the implicit conversions the real type of the
object
| is capable of. I know I could implement it by searching typeof(object) and
| typeof(target) for suitable implicit conversions, but I'm hoping there's a
| method already implemented.
|
| "Shardool Karnik" wrote:
| > try
| >
| > System.Reflection.PropertyInfo pi =
| > Type.GetType("Holder").GetProperty("Property");
| >
| > pi.SetValue(h, "VALUE", null);
| >
| >
message
| > | > > Given the classes:
| > >
| > > class Class
| > > {
| > > public static implicit operator int(Class c)
| > > {
| > > return 0;
| > > }
| > > }
| > > class Holder
| > > {
| > > public int Property
| > > {
| > > set {}
| > > }
| > > }
| > >
| > > I can write the code:
| > >
| > > Holder h = new Holder();
| > > h.Property = new Class();
| > >
| > > How can I do the same thing using reflection, without making any
changes
| > to
| > > the classes used?
| > >
| > > I have tried:
| > >
| > > System.Reflection.PropertyInfo pi =
| > > Type.GetType("Holder").GetProperty("Property");
| > > pi.SetValue(h, c, System.Reflection.BindingFlags.SetProperty,
| > > null, new object[] {}, null);
| > >
| > > An exception gets thrown:
| > >
| > > An unhandled exception of type 'System.ArgumentException'
| > > occurred in mscorlib.dll
| > >
| > > Additional information: Cannot widen from target type to
| > > primitive type.
| > >
| >
| >
| >
 
J

Jay B. Harlow [MVP - Outlook]

Aaron,
The compile knows how to do the implicit conversion, as the compiler knows
the type involved. It calls op_Implicit on that type to do the conversion.

Using reflection you could look for op_Implicit method on your object & call
it. Remember that op_Implicit & op_Explicit both may be overloaded based on
return type, be certain you select the correct op_Implicit via reflection.
Unfortunately I don't have a working example.

I would favor the IConvertible & Convert.ChangeType method as it seems
"cleaner", especially if you control the types involved...

Hope this helps
Jay

| Convert.ChangeType seems to need the IConvertible interface on the
objects,
| and I can't (and shouldn't need to) change the objects.
|
| The compiler knows how to do the implicit conversions, and I could
implement
| the compiler's logic, but it would be great if there was an easy way to do
| the implicit conversion automatically.
|
| TypeConverter throws the following exception:
|
| An unhandled exception of type 'System.NotSupportedException'
| occurred in system.dll
|
| Additional information: 'TypeConverter' is unable to convert
| 'Class' to 'System.Int32'.
|
| "Jay B. Harlow [MVP - Outlook]" wrote:
| > Aaron,
| > Have you looked at Convert.ChangeType & the IConvertible interface?
| >
| > Class would implement IConvertible providing ToInt32 in this case, it
may
| > need to provide Convert.ToType in other cases.
| >
| > Class c = ...;
| > Holder h = ...;
| >
| > | > System.Reflection.PropertyInfo pi =
| > h.GetType().GetProperty("Property");
| > | >
| > Object v = Convert.ChangeType(c, pi.PropertyType);
| > | > pi.SetValue(h, v , null);
| >
| >
| > Another option may be System.ComponentModel & its TypeConverter
framework,
| > however I have not done a lot with TypeConverters...
| >
| > Hope this helps
| > Jay
| >
message
| > | > | I've tried that too, but got the same result. :)
| > |
| > | Given a System.Type, I need some way to convert any object to that
type at
| > | runtime, while honouring the implicit conversions the real type of the
| > object
| > | is capable of. I know I could implement it by searching typeof(object)
and
| > | typeof(target) for suitable implicit conversions, but I'm hoping
there's a
| > | method already implemented.
| > |
| > | "Shardool Karnik" wrote:
| > | > try
| > | >
| > | > System.Reflection.PropertyInfo pi =
| > | > Type.GetType("Holder").GetProperty("Property");
| > | >
| > | > pi.SetValue(h, "VALUE", null);
| > | >
| > | >
| > message
| > | > | > | > > Given the classes:
| > | > >
| > | > > class Class
| > | > > {
| > | > > public static implicit operator int(Class c)
| > | > > {
| > | > > return 0;
| > | > > }
| > | > > }
| > | > > class Holder
| > | > > {
| > | > > public int Property
| > | > > {
| > | > > set {}
| > | > > }
| > | > > }
| > | > >
| > | > > I can write the code:
| > | > >
| > | > > Holder h = new Holder();
| > | > > h.Property = new Class();
| > | > >
| > | > > How can I do the same thing using reflection, without making any
| > changes
| > | > to
| > | > > the classes used?
| > | > >
| > | > > I have tried:
| > | > >
| > | > > System.Reflection.PropertyInfo pi =
| > | > > Type.GetType("Holder").GetProperty("Property");
| > | > > pi.SetValue(h, c, System.Reflection.BindingFlags.SetProperty,
| > | > > null, new object[] {}, null);
| > | > >
| > | > > An exception gets thrown:
| > | > >
| > | > > An unhandled exception of type 'System.ArgumentException'
| > | > > occurred in mscorlib.dll
| > | > >
| > | > > Additional information: Cannot widen from target type to
| > | > > primitive type.
| > | > >
| > | >
| > | >
| > | >
| >
| >
| >
 
S

scott.yarman

I had to do this very thing when I was writing some data mapping code.
Here is the code I came up with:

try
{
value = Convert.ChangeType(value,targetType);
}
catch (System.Exception e)
{
// search for an implicit conversion operator that satisifies
// the type conversion
MethodInfo[] methods = targetType.GetMethods();
for (int i = 0; i < methods.Length; i++)
{
if (methods.Name == "op_Implicit")
{
ParameterInfo[] parameters = methods.GetParameters();
if (parameters.Length == 1 &&
parameters[0].ParameterType == value.GetType())
{
value = methods.Invoke(obj,new object[]{value});
break;
}
}
}
}
 
G

Guest

The op_Implicit method will do the conversion, and certainly Scott's example
has the basic logic, but the C# compiler does more:

- It looks at all op_Implicit implementations in the source and target
classes.
- Then, looks for the return value T that has the most appropriate implicit
conversion to the target type and the argument type A that has the most
appropriate implicit conversion to the source type.
- Finally, it looks for "T op_Implicit(A)" and succeeds if one and only one
instance exists.

Now, I could implement this, but if there is part of the .NET Framework that
will do it for me, that would be better. I'm really considering compiling on
the fly because it would a) do the job, and b) improve efficiency if I need
to do it more than once.

Thanks,
Aaron.

Jay B. Harlow said:
Aaron,
The compile knows how to do the implicit conversion, as the compiler knows
the type involved. It calls op_Implicit on that type to do the conversion.

Using reflection you could look for op_Implicit method on your object & call
it. Remember that op_Implicit & op_Explicit both may be overloaded based on
return type, be certain you select the correct op_Implicit via reflection.
Unfortunately I don't have a working example.

I would favor the IConvertible & Convert.ChangeType method as it seems
"cleaner", especially if you control the types involved...

Hope this helps
Jay

| Convert.ChangeType seems to need the IConvertible interface on the
objects,
| and I can't (and shouldn't need to) change the objects.
|
| The compiler knows how to do the implicit conversions, and I could
implement
| the compiler's logic, but it would be great if there was an easy way to do
| the implicit conversion automatically.
|
| TypeConverter throws the following exception:
|
| An unhandled exception of type 'System.NotSupportedException'
| occurred in system.dll
|
| Additional information: 'TypeConverter' is unable to convert
| 'Class' to 'System.Int32'.
|
| "Jay B. Harlow [MVP - Outlook]" wrote:
| > Aaron,
| > Have you looked at Convert.ChangeType & the IConvertible interface?
| >
| > Class would implement IConvertible providing ToInt32 in this case, it
may
| > need to provide Convert.ToType in other cases.
| >
| > Class c = ...;
| > Holder h = ...;
| >
| > | > System.Reflection.PropertyInfo pi =
| > h.GetType().GetProperty("Property");
| > | >
| > Object v = Convert.ChangeType(c, pi.PropertyType);
| > | > pi.SetValue(h, v , null);
| >
| >
| > Another option may be System.ComponentModel & its TypeConverter
framework,
| > however I have not done a lot with TypeConverters...
| >
| > Hope this helps
| > Jay
| >
message
| > | > | I've tried that too, but got the same result. :)
| > |
| > | Given a System.Type, I need some way to convert any object to that
type at
| > | runtime, while honouring the implicit conversions the real type of the
| > object
| > | is capable of. I know I could implement it by searching typeof(object)
and
| > | typeof(target) for suitable implicit conversions, but I'm hoping
there's a
| > | method already implemented.
| > |
| > | "Shardool Karnik" wrote:
| > | > try
| > | >
| > | > System.Reflection.PropertyInfo pi =
| > | > Type.GetType("Holder").GetProperty("Property");
| > | >
| > | > pi.SetValue(h, "VALUE", null);
| > | >
| > | >
| > message
| > | > | > | > > Given the classes:
| > | > >
| > | > > class Class
| > | > > {
| > | > > public static implicit operator int(Class c)
| > | > > {
| > | > > return 0;
| > | > > }
| > | > > }
| > | > > class Holder
| > | > > {
| > | > > public int Property
| > | > > {
| > | > > set {}
| > | > > }
| > | > > }
| > | > >
| > | > > I can write the code:
| > | > >
| > | > > Holder h = new Holder();
| > | > > h.Property = new Class();
| > | > >
| > | > > How can I do the same thing using reflection, without making any
| > changes
| > | > to
| > | > > the classes used?
| > | > >
| > | > > I have tried:
| > | > >
| > | > > System.Reflection.PropertyInfo pi =
| > | > > Type.GetType("Holder").GetProperty("Property");
| > | > > pi.SetValue(h, c, System.Reflection.BindingFlags.SetProperty,
| > | > > null, new object[] {}, null);
| > | > >
| > | > > An exception gets thrown:
| > | > >
| > | > > An unhandled exception of type 'System.ArgumentException'
| > | > > occurred in mscorlib.dll
| > | > >
| > | > > Additional information: Cannot widen from target type to
| > | > > primitive type.
| > | > >
| > | >
| > | >
| > | >
| >
| >
| >
 
J

Jay B. Harlow [MVP - Outlook]

Aaron,
True: I was considering Int32 & Class only there are no op_Implicit on
Int32.

Compiling on the fly may be the way to go, as there may be opcodes involved
(Double & Int32)?

Plus as you hint, in .NET 2.0 there may be generic conversions involved...

Hope this this helps
Jay

| The op_Implicit method will do the conversion, and certainly Scott's
example
| has the basic logic, but the C# compiler does more:
|
| - It looks at all op_Implicit implementations in the source and target
| classes.
| - Then, looks for the return value T that has the most appropriate
implicit
| conversion to the target type and the argument type A that has the most
| appropriate implicit conversion to the source type.
| - Finally, it looks for "T op_Implicit(A)" and succeeds if one and only
one
| instance exists.
|
| Now, I could implement this, but if there is part of the .NET Framework
that
| will do it for me, that would be better. I'm really considering compiling
on
| the fly because it would a) do the job, and b) improve efficiency if I
need
| to do it more than once.
|
| Thanks,
| Aaron.
|
| "Jay B. Harlow [MVP - Outlook]" wrote:
| > Aaron,
| > The compile knows how to do the implicit conversion, as the compiler
knows
| > the type involved. It calls op_Implicit on that type to do the
conversion.
| >
| > Using reflection you could look for op_Implicit method on your object &
call
| > it. Remember that op_Implicit & op_Explicit both may be overloaded based
on
| > return type, be certain you select the correct op_Implicit via
reflection.
| > Unfortunately I don't have a working example.
| >
| > I would favor the IConvertible & Convert.ChangeType method as it seems
| > "cleaner", especially if you control the types involved...
| >
| > Hope this helps
| > Jay
| >
message
| > | > | Convert.ChangeType seems to need the IConvertible interface on the
| > objects,
| > | and I can't (and shouldn't need to) change the objects.
| > |
| > | The compiler knows how to do the implicit conversions, and I could
| > implement
| > | the compiler's logic, but it would be great if there was an easy way
to do
| > | the implicit conversion automatically.
| > |
| > | TypeConverter throws the following exception:
| > |
| > | An unhandled exception of type 'System.NotSupportedException'
| > | occurred in system.dll
| > |
| > | Additional information: 'TypeConverter' is unable to convert
| > | 'Class' to 'System.Int32'.
| > |
| > | "Jay B. Harlow [MVP - Outlook]" wrote:
| > | > Aaron,
| > | > Have you looked at Convert.ChangeType & the IConvertible interface?
| > | >
| > | > Class would implement IConvertible providing ToInt32 in this case,
it
| > may
| > | > need to provide Convert.ToType in other cases.
| > | >
| > | > Class c = ...;
| > | > Holder h = ...;
| > | >
| > | > | > System.Reflection.PropertyInfo pi =
| > | > h.GetType().GetProperty("Property");
| > | > | >
| > | > Object v = Convert.ChangeType(c, pi.PropertyType);
| > | > | > pi.SetValue(h, v , null);
| > | >
| > | >
| > | > Another option may be System.ComponentModel & its TypeConverter
| > framework,
| > | > however I have not done a lot with TypeConverters...
| > | >
| > | > Hope this helps
| > | > Jay
| > | >
| > message
| > | > | > | > | I've tried that too, but got the same result. :)
| > | > |
| > | > | Given a System.Type, I need some way to convert any object to that
| > type at
| > | > | runtime, while honouring the implicit conversions the real type of
the
| > | > object
| > | > | is capable of. I know I could implement it by searching
typeof(object)
| > and
| > | > | typeof(target) for suitable implicit conversions, but I'm hoping
| > there's a
| > | > | method already implemented.
| > | > |
| > | > | "Shardool Karnik" wrote:
| > | > | > try
| > | > | >
| > | > | > System.Reflection.PropertyInfo pi =
| > | > | > Type.GetType("Holder").GetProperty("Property");
| > | > | >
| > | > | > pi.SetValue(h, "VALUE", null);
| > | > | >
| > | > | >
in
| > | > message
| > | > | > | > | > | > > Given the classes:
| > | > | > >
| > | > | > > class Class
| > | > | > > {
| > | > | > > public static implicit operator int(Class c)
| > | > | > > {
| > | > | > > return 0;
| > | > | > > }
| > | > | > > }
| > | > | > > class Holder
| > | > | > > {
| > | > | > > public int Property
| > | > | > > {
| > | > | > > set {}
| > | > | > > }
| > | > | > > }
| > | > | > >
| > | > | > > I can write the code:
| > | > | > >
| > | > | > > Holder h = new Holder();
| > | > | > > h.Property = new Class();
| > | > | > >
| > | > | > > How can I do the same thing using reflection, without making
any
| > | > changes
| > | > | > to
| > | > | > > the classes used?
| > | > | > >
| > | > | > > I have tried:
| > | > | > >
| > | > | > > System.Reflection.PropertyInfo pi =
| > | > | > > Type.GetType("Holder").GetProperty("Property");
| > | > | > > pi.SetValue(h, c, System.Reflection.BindingFlags.SetProperty,
| > | > | > > null, new object[] {}, null);
| > | > | > >
| > | > | > > An exception gets thrown:
| > | > | > >
| > | > | > > An unhandled exception of type 'System.ArgumentException'
| > | > | > > occurred in mscorlib.dll
| > | > | > >
| > | > | > > Additional information: Cannot widen from target type to
| > | > | > > primitive type.
| > | > | > >
| > | > | >
| > | > | >
| > | > | >
| > | >
| > | >
| > | >
| >
| >
| >
 
G

Guest

If I'm reading the spec correctly, both the source and target types can
provide the implicit conversion, and op_Implicit must have one argument with
either the argument or return type being the type of the class to which
op_Implicit belongs, so:

object obj = null;
for (int i = 0; i < methods.Length; i++)
{
if ( methods.Name == "op_Implicit"
&& methods.ReturnType == targetType )
{
obj = methods.Invoke(null, new object[]{c});
break;
}
}

if (obj == null)
{
methods = targetType.GetMethods();
for (int i = 0; i < methods.Length; i++)
{
if ( methods.Name == "op_Implicit"
&& methods.GetParameters()[0].ParameterType == valueType )
{
obj = methods.Invoke(null, new object[]{c});
break;
}
}
}

would cope with both cases, but it still doesn't cope with the fact that
implicit standard conversions can be made, i.e. number widening or reference
conversions, nor does it handle duplicate suitable op_Implicit instances, so
I'll need to add that functionality. I'm sure I'd be reinventing the wheel
because the C# compiler already handles all that logic.
 

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