Re: Type convertsion from string



NRE:
You are using the method-lookup from my code ("SomeBodyOfCode"), but
with your class name ("EntityBase") - i.e. you have mis-translated the
code I posted. Instead of using the generics trick (as suggested) you
are sticking with reflection. Fine, it'll work, but it is much harder.
Just use:
Customer cust = (Customer) Activator.CreateInstance(dynamicType) and
it should work - cust will be an instance of CustomerExtension, which
inherits from Customer so the cast will work - *assuming* that you got
the references correct when compiling the dynamic assembly. This
*must* be the same Customer type - it is not sufficient to copy the
Customer etc code into the dynamic assembly; this will be a different
Customer class and the cast will fail.

Which all brings me back to my main point: dynamically building code
from source like this is messy! Especially with reflection thrown
in...

How to create TypeDescriptionProvider which compiles poperty
expression to static method and executes it in the fly?
This would be good solution which allows creating dynamic assemblies
only for extensible properites.
Personally, that isn't the way I would approach it. I would use a
parser (like on of those already cited) to find something that I can
invoke - but that doesn't necessarily mean compiling. I would use
TypeDescriptionProvider to add a runtime-only PropertyDescriptor into
the mix, and in the GetValue etc I would invoke the above expression.
No need for compiling; no need for dynamic types - just the types you
already defined in your object model. Just smart use of the
"System.ComponentModel" area. And likewise for additional read/write
data properties; put a property-bag data-store[*1] into the EntityBase
object model (Dictionary<string,object> would do), and somewhere
(prefereably static) to keep the track of the extra properties[*2],
and just add in PropertyDescriptors (from the list in [*2]) whose
GetValue and SetValue talk the the instance's property-bag (in [*1]).

The above is a little more complex than some code, but I would expect
(from experience) it to be a lot more robust than messing with
compiling code (especially user-code) and hooking it all together with
reflection.

Marc


.



Relevant Pages

  • Re: Real world modeling.
    ... > The second has a Cargo object with a Route Specification. ... It's a year later and the customer wants to enhance the ... Which scenerio would make it easier, ... Then someone requests a feature "Save skip compiling". ...
    (comp.object)
  • Re: OT Kinda! Reflections connection to VMS :-)
    ... I was recently told by a customer that a simple SMG ... generated menu doesn't display properly in Reflection 11. ... I don't know about PuTTY. ...
    (comp.os.vms)
  • Re: procomp compiler license question
    ... privillages unavailable to install software) ... Why will the customer be compiling their code? ...
    (comp.lang.tcl)
  • Re: OT Kinda! Reflections connection to VMS :-)
    ... I am making a connection to multiple OpenVMS boxes and the firewall has an idle connection killer that is really aggressive. ... With PuTTY I can set keep alives for once a minute and I am OK, but for the life of me with Reflection V11.0 I can not find a similar setting. ... I was recently told by a customer that a simple SMG ...
    (comp.os.vms)
  • Re: The Borland problem
    ... It looks like a Wall Street Journal site. ... Simply a reflection of the type of customer they are now trying to reach. ... Check out the Delphi Community Blog Aggregator ...
    (borland.public.delphi.non-technical)