开发者

Anonymous Types

I have a Dictionary(TKey, TValue) like

Dictionary<int, ArrayList> Deduction_Employees = 
    new Dictionary<int, ArrayList>();

and later I add to that array list an anonymous type like this

var day_and_ty开发者_开发百科pe = new {
    TheDay = myDay,
    EntranceOrExit = isEntranceDelay
};

Deduction_Employees[Employee_ID].Add(day_and_type);

Now how can I unbox that var and access those properties ??


First, you aren't unboxing the type. Anonymous types are reference types, not structures.

Even though you can technically create instances of the same type outside of the method they were declared in (as per section 7.5.10.6 of the C# 3.0 Language Specification, which states:

Within the same program, two anonymous object initializers that specify a sequence of properties of the same names and compile-time types in the same order will produce instances of the same anonymous type.

) you have no way of getting the name of the type, which you need in order to perform the cast from Object back to the type you created. You would have to resort to a cast-by-example solution which is inherently flawed.

Cast-by-example is flawed because from a design standpoint, every single place you want to access the type outside the function it is declared (and still inside the same module), you have to effectively declare the type all over again.

It's a duplication of effort that leads to sloppy design and implementation.

If you are using .NET 4.0, then you could place the object instance in a dynamic variable. However, the major drawback is the lack of compile-time verification of member access. You could easily misspell the name of the member, and then you have a run-time error instead of a compile-time error.

Ultimately, if you find the need to use an anonymous type outside the method it is declared in, then the only good solution is to create a concrete type and substitute the anonymous type for the concrete type.


There are several ways.

Since the comments seems to indicate that I suggest you do this, let me make it clear: You should be creating a named type for your object since you intend to pass it around.

First, you can use Reflection, which another answer here has already pointed out.

Another way, which tricks .NET into giving you the right type is known as "cast by example", and it goes something like this: You need to pass your object through a generic method call, which will return the object as the right type, by inferring the right type to return.

For instance, try this:

private static T CastByExample<T>(T example, object value)
{
    return (T)value;
}

and to use it:

var x = CastByExample(new { TheDay = ??, EntranceOrExit = ?? }, obj);

for the two ?? spots, you just need to pass something fitting the data type for those properties, the values will not be used.

This exploits the fact that multiple anonymous types containing the exact same properties, of the same type, in the same order, in the same assembly, will map to the same single type.

However, by this time you should be creating a named type instead.


An anonymous type has method scope. To pass an anonymous type, or a collection that contains anonymous types, outside a method boundary, you must first cast the type to object. However, this defeats the strong typing of the anonymous type. If you must store your query results or pass them outside the method boundary, consider using an ordinary named struct or class instead of an anonymous type.

Source: http://msdn.microsoft.com/en-us/library/bb397696.aspx


No you can't. You can only access the properties by using reflection. The compiler has no way of knowing what the type was, and since it's an anonymous type, you can't cast it either.


If you are using .NET 1.x - 3.x, you must use reflection.

If you use .NET 4.0, you could use a dynamic type and call the expected properties.

In neither case do you need to unbox; that's for value types. Anonymous types are always reference types.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜