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.
| > | > | > >
| > | > | >
| > | > | >
| > | > | >
| > | >
| > | >
| > | >
| >
| >
| >