You went to a lot of work to refactor away from simple, readable code to
something much more complicated and much less readable. Were there bugs in
the original code?
Maybe I'm just a bit more jaded than you are. When I see a need for
something that I've done a dozen times before, I either use a strategy
pattern or simply encapsulation. Not dissimiliar to your original code.
Reflection, as part of fundamental code that is Likely to run in a loop,
just seems like a convenience that is ill afforded.
By now, I'm sure that you know that Generics are coming in the next version
of .Net. You appear too advanced to have missed that bit of news.
So, in the current code, you could be coding your classes so that they are
easily replaced with generic classes when they become available. Or you
could code for readability, so that the person who gets to maintain this
code can easily resolve defects.
This code does neither. It is clever, true. But I see no value over the
previous incarnation.
Sorry if this isn't what you wanted to hear.
As for performance, only testing will tell. I cannot imagine your code will
perform as well after this change. If it is still of "acceptable" speed
will depend on your app.
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik
Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"MattC" <m@m.com> wrote in message
news:Or**************@TK2MSFTNGP10.phx.gbl...
Hi,
I have a collection class derived from ArrayList, it stores colletions of
objects from my objet model. I Didn't want to have to create a
specialised collection for each object type so I created the following static method.
public static MyCollection CreateGenericCollection(DataTable rates,
Assembly a, Type t)
{
MyCollection c = new MyCollection(); // create new collection
Type[] types = new Type[1];
types[0] = typeof(DataRow); //all my BLL object have to take a DataRow
in a constructor
object obj=Activator.CreateInstance(t); //get the type of the object I
am creating collection of
ConstructorInfo ci = t.GetConstructor(types); //get its constructor
info object[] p=new object[1];
foreach(DataRow row in rates.Rows) //convert to for loop later
{
p[0] = row;
c.Add(ci.Invoke(p)); //invoke the constructor passing in the row and
add to Arraylist
}
return c; //send back filled collection when done?
}
Oringinally I had each BLL object to something like this.
public static MyCollection CreateLabourRateCollection(DataTable rates)
{
MyCollection c = new MyCollection();
foreach(DataRow row in rates.Rows)
{
c.Add(new LabourRate(row));
}
return c;
}
Ok my question.
How slow is Reflection given that the Assemblies to lookup are all located
in the same place?
Also any suggestions on the method would be cool, thanks.
TIA
MattC