On the fly type generation
Forum » Nvigorate Design / Specific Design » On the fly type generation
Started by: mechheadmechhead
On: 1173563594|%e %b %Y, %H:%M %Z|agohover
Number of posts: 4
rss icon RSS: New posts
Summary:
whats the basic idea behind this?
On the fly type generation
mechheadmechhead 1173563594|%e %b %Y, %H:%M %Z|agohover

Hey Alex,
I've been nosing around the Reflection class you posted, interesting stuff so far. I have a question about the planned features, especially about the 'on the fly' generation of types from various inputs. How generic are you able to make this? I mean, I can imagine creating objects froma DataTable like this (i'm using some NVigorate functions in the code below):

public static object[] CreateObjectsFromDataTable(DataTable aTable, Type aType)
{
    object[] ObjList = new object[aTable.Rows.Count];
    for(int i=0;i<aTable.Rows.Count;i++)
    {
        ObjList[i] = CreateObjectFromDataRow(aTable.Rows[i], aType);
    }
    return ObjList;
}

public static object CreateObjectFromDataRow(DataRow aRow, Type aType)
{
    ConstructorInfo aConstrInf = aType.GetConstructor(new Type[0]);
    if (aConstrInf.GetParameters().Length > 0)
    {
        Throw(String.Format("{0} does not have a parameterless contructor", aType.FullName));
    }
    object aObj = aConstrInf.Invoke(new object[0]);
    foreach (DataColumn aColumn in aRow.Table.Columns)
    {
        WriteProperty(aObj, aColumn.ColumnName, aRow[aColumn.ColumnName], new object[0]);
    }
    return aObj;
}

In this example I'm assuming that the column names are the same as the property names in the targeted type. Should they not be the same then I guess the DataTable would need additional 'massaging' before its passed to the function.
Is this roughly the type of strategy you have in mind? So how would this work for filling an object from XML? Would you simply pass in a node collection that uses the element's name as the property binding?

Anyways, I think the Reflection class shows great potential for all sorts of object factory type of work, I'll be watching NVigorate's progress closely. Thank you very much for your efforts so far.

cheers,

Roland

unfold On the fly type generation by mechheadmechhead, 1173563594|%e %b %Y, %H:%M %Z|agohover
Re: On the fly type generation
alexexmachinaalexexmachina 1173630518|%e %b %Y, %H:%M %Z|agohover

Hi Roland,

That's a very good example of a simple factory and it's a very large reason why I wrote Reflector to begin with. But I'm talking about something a little crazier that I've done in the past where I actually define a class type on the fly using the Reflection.Emit namespace. You're actually creating a CLR object at runtime in memory and then you can take the resulting assembly and save it to disk. The trick part is that the system/application utilizing these has to have some kind of permanent caching system so that it knows that an object with X parameters of the specific types = this assembly that was created on the fly and saved last time. That's to prevent needless re-creation of an assembly which just has the same type. This is also because if you try and create the same CLR object in memory w/ the same name your app *will* crash :)

The code you've demonstrated is actually very close to one way in which IBinder and IMapper classes work together. IBinder essentially copies data from one source to another, but it needs mapping information which is provided by IMapper. These two Interfaces are abstract enough that the implementations will do things like binding data table information to class instances, like you've demonstrated.

I'm very glad that you can see the usefullness of the class, that lets me know that I'm on the right track!

Thanks,

Alex

unfold Re: On the fly type generation by alexexmachinaalexexmachina, 1173630518|%e %b %Y, %H:%M %Z|agohover
Re: On the fly type generation
mechheadmechhead 1173776099|%e %b %Y, %H:%M %Z|agohover

Hey there again Alex,

thats funny, I was just reading an article over on codeproject about the Reflection.Emit namespace and was immediately thinking about NVigorate haha. I wasn't aware yet of this .NET functionality, but it is pretty awesome. It would indeed be a very powerful feature for NVigorate to encapsulate this functionality, because I'm sure there's plenty of programmers who aren't that enthusiastic about passing around .NET opcodes :)
Another interesting implementation would be for the System.CodeDom namespace, so instead of a fully working type a programmer can just generate the source code for later compilation.

You're definitely on the right track here, keep us posted and thanks a lot so far!

cheers,

Roland

last edited on 1173776168|%e %b %Y, %H:%M %Z|agohover by mechhead + show more
unfold Re: On the fly type generation by mechheadmechhead, 1173776099|%e %b %Y, %H:%M %Z|agohover
Re: On the fly type generation
mechheadmechhead 1173776818|%e %b %Y, %H:%M %Z|agohover

Oh, and in case you were wondering, these are the codeproject articles I was refering to:

http://www.codeproject.com/dotnet/Creating_Dynamic_Types.asp

http://www.codeproject.com/useritems/Creating_Dynamic_Types2.asp

Also for anyone else who's interested in this subject matter, its a good read for an introduction into the Reflection.Emit namespace

unfold Re: On the fly type generation by mechheadmechhead, 1173776818|%e %b %Y, %H:%M %Z|agohover
New post
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.