By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,709 Members | 1,650 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,709 IT Pros & Developers. It's quick & easy.

Setting a property by reflection, with implicit conversion

P: n/a
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.

Nov 17 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
try

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

pi.SetValue(h, "VALUE", null);
"Aaron Queenan" <Aa**********@discussions.microsoft.com> wrote in message
news:42**********************************@microsof t.com...
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.

Nov 17 '05 #2

P: n/a
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);
"Aaron Queenan" <Aa**********@discussions.microsoft.com> wrote in message
news:42**********************************@microsof t.com...
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.


Nov 17 '05 #3

P: n/a
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" <Aa**********@discussions.microsoft.com> wrote in message
news:01**********************************@microsof t.com...
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);
"Aaron Queenan" <Aa**********@discussions.microsoft.com> wrote in message news:42**********************************@microsof t.com...
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.


Nov 17 '05 #4

P: n/a
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

"Aaron Queenan" <Aa**********@discussions.microsoft.com> wrote in message
news:01**********************************@microsof t.com...
| 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);
| >
| >
| > "Aaron Queenan" <Aa**********@discussions.microsoft.com> wrote in
message
| > news:42**********************************@microsof t.com...
| > > 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.
| > >
| >
| >
| >
Nov 17 '05 #5

P: n/a
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" wrote:
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" <Aa**********@discussions.microsoft.com> wrote in message
news:01**********************************@microsof t.com...
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);
"Aaron Queenan" <Aa**********@discussions.microsoft.com> wrote in message news:42**********************************@microsof t.com...
> 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.
>


Nov 17 '05 #6

P: n/a
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

"Aaron Queenan" <Aa**********@discussions.microsoft.com> wrote in message
news:01**********************************@microsof t.com...
| 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);
| >
| >
| > "Aaron Queenan" <Aa**********@discussions.microsoft.com> wrote in
message
| > news:42**********************************@microsof t.com...
| > > 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.
| > >
| >
| >
| >

Nov 17 '05 #7

P: n/a
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

"Aaron Queenan" <Aa**********@discussions.microsoft.com> wrote in message
news:31**********************************@microsof t.com...
| 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
| >
| > "Aaron Queenan" <Aa**********@discussions.microsoft.com> wrote in
message
| > news:01**********************************@microsof t.com...
| > | 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);
| > | >
| > | >
| > | > "Aaron Queenan" <Aa**********@discussions.microsoft.com> wrote in
| > message
| > | > news:42**********************************@microsof t.com...
| > | > > 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.
| > | > >
| > | >
| > | >
| > | >
| >
| >
| >
Nov 17 '05 #8

P: n/a
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[i].Name == "op_Implicit")
{
ParameterInfo[] parameters = methods[i].GetParameters();
if (parameters.Length == 1 &&
parameters[0].ParameterType == value.GetType())
{
value = methods[i].Invoke(obj,new object[]{value});
break;
}
}
}
}

Nov 17 '05 #9

P: n/a
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

"Aaron Queenan" <Aa**********@discussions.microsoft.com> wrote in message
news:31**********************************@microsof t.com...
| 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
| >
| > "Aaron Queenan" <Aa**********@discussions.microsoft.com> wrote in
message
| > news:01**********************************@microsof t.com...
| > | 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);
| > | >
| > | >
| > | > "Aaron Queenan" <Aa**********@discussions.microsoft.com> wrote in
| > message
| > | > news:42**********************************@microsof t.com...
| > | > > 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.
| > | > >
| > | >
| > | >
| > | >
| >
| >
| >

Nov 17 '05 #10

P: n/a
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

"Aaron Queenan" <Aa**********@discussions.microsoft.com> wrote in message
news:60**********************************@microsof t.com...
| 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
| >
| > "Aaron Queenan" <Aa**********@discussions.microsoft.com> wrote in
message
| > news:31**********************************@microsof t.com...
| > | 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
| > | >
| > | > "Aaron Queenan" <Aa**********@discussions.microsoft.com> wrote in
| > message
| > | > news:01**********************************@microsof t.com...
| > | > | 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);
| > | > | >
| > | > | >
| > | > | > "Aaron Queenan" <Aa**********@discussions.microsoft.com> wrote
in
| > | > message
| > | > | > news:42**********************************@microsof t.com...
| > | > | > > 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.
| > | > | > >
| > | > | >
| > | > | >
| > | > | >
| > | >
| > | >
| > | >
| >
| >
| >
Nov 17 '05 #11

P: n/a
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[i].Name == "op_Implicit"
&& methods[i].ReturnType == targetType )
{
obj = methods[i].Invoke(null, new object[]{c});
break;
}
}

if (obj == null)
{
methods = targetType.GetMethods();
for (int i = 0; i < methods.Length; i++)
{
if ( methods[i].Name == "op_Implicit"
&& methods[i].GetParameters()[0].ParameterType == valueType )
{
obj = methods[i].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.

"sc**********@gmail.com" wrote:
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[i].Name == "op_Implicit")
{
ParameterInfo[] parameters = methods[i].GetParameters();
if (parameters.Length == 1 &&
parameters[0].ParameterType == value.GetType())
{
value = methods[i].Invoke(obj,new object[]{value});
break;
}
}
}
}

Nov 17 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.