Then I call it as (this part is called millions of times):
Most of the examples you'll see call typeof(Enumerable).GetMethod("Cast"), but if you look at that in reflector, you'll see it does a few useless checks and then calls CastIterator. To save time on the useless checks and the extra method call, I call CastIterator directly.
Any other options? EnumFactory is, of course, cached. If I comment out the part that fills in oArray, its negligible compared to calling EnumFactory. I do not know T at compile time.
I don't know T until runtime, so I would have to do the same thing of making a generic method and calling it through a delegate / invoke / expression tree.
However, the above code you suggested will actually be "much" slower then Cast / CastIterator since you are new'ing up the T array every time. CastIterator is simply:
// System.Linq.Enumerableprivatestatic IEnumerable<TResult> CastIterator<TResult>(IEnumerable source)
foreach (object current in source)
Unless of course, the yield keyword is expensive... but from my understanding of how it works internally, it should be MUCH, MUCH faster then the method above since its lazy (and it doesn't new up anything).
I know I'm nitpicking lol, but really trying to squeeze everything out of this block of code.
If you don't know the type until runtime, why not make it IEnumerable<dynamic>. When you run this, it will appear that you have an IEnumerable of objects (as you have now), but with the proviso that because it's dynamic, it will honour any method calls you care to make on that object. I haven't tested the performance of this, but that might be worth a try for you.
Hmm... I need to return a strongly typed IEnumerable. If I return an IEnumerable<dynamic> off of the object, it compiles, but I get a runtime cast error. I don't have control over the calling code. I'm just the library that serves it up.
That may be, but unless you try it (and/or the Enumerator you show) you'll never know for sure -- you're only acting on hearsay or prejudice. Get a baseline reading, then try something else and get another reading, etc. You're assuming that what you have is "much" faster, but it may not be, you need metrics.
What are you trying to do?
Why are you accepting an object of them; can you correct this upstream?
And what actual types are involved? Value types? Structs? Small simple classes? Large complex classes?
Laziness may not help much unless the consumer doesn't need all the members.
That may be, but unless you try it (and/or the Enumerator you show) you'll never
know for sure -- you're only acting on hearsay or prejudice. Get a baseline
reading, then try something else and get another reading, etc. You're assuming
that what you have is "much" faster, but it may not be, you need metrics.
What are you trying to do? Why are you accepting an object of
them; can you correct this upstream? And what actual types are involved?
Value types? Structs? Small simple classes? Large complex classes?
Ok ... well, for the sake of the thread, I did... its not "much, much slower" as I thought it would be... but it is quite a bit slower (about 15%). 3900ms vs 4500ms
What I'm trying to do is given an array of some unknown (at compile time) object (which is why I store them as object), I need to convert said object into an IEnumerable T where T is known at compile time by the caller of my class.
I could potentially store a T, but unfortunately, my class has non-T overloads. So its kind of like I need to support:
IEnumerable<SomeClass> enum = GetTheObjects<SomeClass>();
I have a WINFORM that displays a treenode and a datagridview. I want selected items from the treenode to go to the datagridview. If I select the parent node of treenode all children go to datagridview but if I just expand the parent and select one of the children, nothing goes to datagrid. (selection of a treenode fires a method to get attributes of the item from sql table and it is the attributes that go to the grid + node value)
When parent is selected 'if (e.Node.Nodes.Count > 0)' is true. When parent isn't selected but child is 'if (e.Node.Nodes.Count > 0)' is false.
So my question is what code do I need to find child nodes that are checked/unchecked?
Once I get correct code to find child node check, what code moves it to datagrid or delete from datagrid if child is unchecked?
// get all child nodes add to dataGridView
DataTable dt = getFieldsTable();
dgvColumns.DataSource = dt;
privatevoid tvFileMan_AfterCheck(object sender, TreeViewEventArgs e)
if (e.Node.Nodes.Count > 0)
// Checked a file so get fields and check all fields except subfiles.// Use this event handler to process actions from check box click
foreach (TreeNode tn in e.Node.Nodes)
tn.Checked = e.Node.Checked;