Re: Type convertsion from string
- From: "Marc Gravell" <marc.gravell@xxxxxxxxx>
- Date: Thu, 18 Oct 2007 07:56:57 +0100
At application start but before accesing entity classes, application
compiles new assembly containing additional properties which
customers may have added to server tables.
(assuming we're still talking about formulae properties)
Fair enough, but you still need to get hold of the new types and
create instances of them. When I have done similar, I have found that
the best approach is actually a generics-trick (see below), since you
then don't need any reflection (except for the initial startup). The
only other options involve some kind of factory, or getting hold of
the ConstructorInfo instances everywhere.
But again, I strongly warn against taking user-entered formulae and
compiling into code! To me, this would rank right up there with
"&admin=false" (as a web-page query-string), or SQL-injection
vulnerability.
If you are only referring to regular value properties (a user-defined
DateTime called "DateOfBirth", etc), then you can do this without all
the messing. Putting together a runtime-extensible (property-bag)
class isn't actually all that hard. I have posted examples to this
forum several times. TypeDescriptionProvider is probably the way to go
in such a case, but it can't handle text formulae unless you bolt a
parser onto the end.
Generics trick to compile with typed code, but execute for a dynamic
(sub)type:
using System;
using System.Reflection;
static class Program
{
static void Main()
{
// would actually be from dynamic assembly
Assembly dynamicAssembly = Assembly.GetExecutingAssembly();
// get the user's type
Type dynamicType = dynamicAssembly.GetType("Dynamic");
// find generic SomeBodyOfCode
MethodInfo mi = typeof(Program).GetMethod(
"SomeBodyOfCode",
BindingFlags.Static | BindingFlags.NonPublic);
// get constructed closed SomeBodyOfCode
mi = mi.MakeGenericMethod(dynamicType);
// invoke
mi.Invoke(null, null);
}
private static void SomeBodyOfCode<T>()
where T : EntityBase, new() {
// can use all EntityBase properties &
// behaviors, new(), etc - but no reflection
// involved
}
}
// from core library
public abstract class EntityBase { }
// from dynamic library
public class Dynamic : EntityBase { }
.
- Follow-Ups:
- Re: Type convertsion from string
- From: Andrus
- Re: Type convertsion from string
- From: Andrus
- Re: Type convertsion 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
- Type convertsion from string
- Prev by Date: Re: A book about the .NET framework
- Next by Date: Prevent decompiling of the code
- Previous by thread: Re: Type convertsion from string
- Next by thread: Re: Type convertsion from string
- Index(es):
Relevant Pages
|