Re: Type convertsion from string
- From: "Marc Gravell" <marc.gravell@xxxxxxxxx>
- Date: Fri, 19 Oct 2007 11:10:25 +0100
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 popertyPersonally, that isn't the way I would approach it. I would use a
expression to static method and executes it in the fly?
This would be good solution which allows creating dynamic assemblies
only for extensible properites.
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
.
- Follow-Ups:
- Re: Type conversion from string
- From: Andrus
- Re: Type conversion from string
- References:
- Type convertsion from string
- From: Andrus
- Re: Type convertsion from string
- From: Jon Skeet [C# MVP]
- Re: Type convertsion from string
- From: Andrus
- Re: Type convertsion from string
- From: Marc Gravell
- Re: Type convertsion from string
- From: Andrus
- Re: Type convertsion from string
- From: Marc Gravell
- Re: Type convertsion from string
- From: Andrus
- Re: Type convertsion from string
- From: Marc Gravell
- Re: Type convertsion from string
- From: Andrus
- Re: Type convertsion from string
- From: Marc Gravell
- Re: Type convertsion from string
- From: Andrus
- Re: Type convertsion from string
- From: Marc Gravell
- Re: Type convertsion from string
- From: Andrus
- Type convertsion from string
- Prev by Date: Re: C# 64-bit DLL?
- Next by Date: Re: C# App Profiling
- Previous by thread: Re: Type convertsion from string
- Next by thread: Re: Type convertsion from string
- Index(es):
Relevant Pages
|