-3

Possible Duplicate:
How can I pass an anonymous type to a method?

Im trying to recognize the type of the anonymous type.

List<int> lst = new List<int> {1, 2, 3, 4, 5};

var myVarType = from item in lst select new {P = item*item, P2 = item + "###"};

foreach (var k in myVarType)
        {
            Console.WriteLine(k.P + "     " + k.P2);
        }

enter image description here

Now i want a part of code to be transferred to a function but it screams that he doesnt know the type - which is logical since var is to be known at compile time and he doesnt know the type at compile :

enter image description here

I dont want to use dynamic || Tuples.

and as you know var is not acceptable as a func param type.

But , Ive once read that there is a trick which lets me transfer to myFunc the anonymous type .

I think it was by Jon skeet or Eric lippert.

Help ?

edit

look at my self answer. I found it here What's the return type of an anonymous class

Community
  • 1
  • 1
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • 3
    The 'trick' is here I think: http://blogs.msdn.com/b/ericlippert/archive/2012/01/23/anonymous-types-unify-within-an-assembly.aspx – H H Feb 01 '12 at 11:03
  • http://stackoverflow.com/questions/775387/passing-a-anonymous-type-to-function <- it pretty much states it's not possible – Yngve B-Nilsen Feb 01 '12 at 11:07
  • @henk I already read this its not the one. there is other code . I will try to find it and let you know – Royi Namir Feb 01 '12 at 11:10
  • @HenkHolterman yeah thats the code. i found it here http://stackoverflow.com/questions/6466054/whats-the-return-type-of-an-anonymous-class/6469199#6469199 – Royi Namir Feb 01 '12 at 11:21
  • @RoyiNamir But it won't let you access any property in a method. So your question is only going to work when casting the dynamic type before. Thats a big garbage. – Felix K. Feb 01 '12 at 11:25
  • @YngveB.Nilsen it is possible see the link – Royi Namir Feb 01 '12 at 11:36

3 Answers3

2

The type is 'generated' and you might be able to get it at run-time with reflection but it will contain characters you can't use in a name.

You could use Tuples:

 select new Tuple<int,string> ( item*item,  item + "###");
H H
  • 263,252
  • 30
  • 330
  • 514
1

Make the method generic, this should work.

static void MyFunc<T>(IEnumerable<T> myVarType) ...

Edit

As mentioned in comments you can't access the properties. You could use here a delegate to access the properties or use dynamic ( which you don't want to use ).

static void MyFunc<T>(IEnumerable<T> myVarType, Func<T, Object[]> argumentCreator)
{
    Console.WriteLine("{0} {1}", argumentCreator(myVarType));
}
Felix K.
  • 6,201
  • 2
  • 38
  • 71
1

here is the code which i found

What's the return type of an anonymous class

static T CastByExample<T>(object source, T example) where T : class
{
    return source as T;
}

static object ReturnsAnonymous() { return new { X = 123 }; }

static void DoIt()
{
    object obj = ReturnsAnonymous();
    var example = new { X = 0 };
    var anon = CastByExample(obj, example);
    Console.WriteLine(anon.X); // 123
}
Community
  • 1
  • 1
Royi Namir
  • 144,742
  • 138
  • 468
  • 792