Perils and Merits of Anonymous classes? Anonymous Descendant class

S

Siegfried Heintze

Can someone help me understand the perils and merits of anonymous classes in
C# 3.0?

Would anonymous classes be most useful when used with reflection? I cannot
figure out how else I would pass them to a function.

How useful is a type if we never pass its instances to a function?

If we do pass an instance of an anonymouse type to a function, can we do
anything with it without using reflection?

The answer to that question might be yes if we can specify an ancestor.
Unfortunately, Andrew Troelsen's book did not show how to create anonymouse
descendant classes.

If I have a class Child, can I create an anonymouse descendant class that
represents a parent that has multiple offspring stored in a linked list and
pass this to a function expecting an instance of class child? Can someone
give me some sample code to do this?

Thanks,
Siegfried
 
N

Nicholas Paldino [.NET/C# MVP]

Siegfried,

Anonymous types are not meant to be passed outside of the
method/property that they are declared in. The scope is limited to that
method. You ^could^ use reflection, but it's really not worth it, you might
as well define your own class at that point and simplify your code.

It's useful because you can create little utility types to aid you in
the operations in that method/property, create projections in queries.

For your specific example, you have to create a concrete Parent type and
have properties that expose the children, then create an instance of that
type and populate the data. This is if you want to use it across different
methods/properties.
 
J

Jon Skeet [C# MVP]

Nicholas Paldino said:
Anonymous types are not meant to be passed outside of the
method/property that they are declared in.

Well, except to generic methods. That's a very important exception,
because otherwise they really would be useless.
 
M

Marc Gravell

It is my guess that they will also have a use for (read-only) views on
data - i.e. get your custom projection into an array/list, and use
that as a DataSource for a list/grid/etc. An indirect reflection
usage...

Maybe ;-p
 
M

Marc Gravell

If I have a class Child, can I create an anonymouse descendant class that
represents a parent that has multiple offspring stored in a linked list and
pass this to a function expecting an instance of class child? Can someone
give me some sample code to do this?

Not directly - but in many cases the common approach is to express the
relationship via a delegate - for example (perhaps; I'm just
speculating here...) something like:

public static void Foo<TParent, TChild>(this IEnumerable<TParent>
parents, Func<TParent, IEnumerable<TChild>> childSelector) {
foreach(TParent parent in parents) {
// do something with parent...
foreach(TChild child in childSelector(parent)) {
// do something with child
}
}
}

You could then call with something *roughly* like:

var orders = {LINQ query}
orders.Foo(x=>x.Lines); // shows the lines per order...

The "real" approach would of course depend on your exact needs... and
ensuring that the compiler can use inference on the API is sometimes
another black art... (but *much* better in C# 3 than C# 2 due to the
inference changes).

Marc
 
N

Nicholas Paldino [.NET/C# MVP]

I should elaborate. Anonymous classes are not intended to be used
^directly^ outside of the method they are declared in. So yes, you can pass
them to generic methods (how else would LINQ work), but you can't access
anything meaningful on them, as they won't satisfy any constraint except the
new constraint. You just have the reference which is passed along in some
way.
 
S

Siegfried Heintze

Can I conclude from the above that it is not possible to create an anonymous
descendant class (except for descendants of Object)?
Thanks,
Siegfried
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top