tags:

views:

227

answers:

4

Hi,

I need to fetch all the properties of an anonmous type which can be written to.

eg:
var person = new {Name = "Person's Name", Age = 25};
Type anonymousType = person.GetType();
var properties = anonymousType.GetProperties(BindingFlags.Public | BindingFlags.Instance);

The problem is that all the properties have their "CanWrite" propert false. This is returned as true for non anonymous types.
I have also tried making a call to PropertyInfo.GetSetMethod() which returns null.
How can I check whether the property can be written to?

Edit:
Perhaps it would suffice to know if a type is anonymous or not. How do I find out if a type is anonymous using reflection?

Kind regards,

+5  A: 

Anonymous types generated from C# are always immutable, so the set of writable properties is empty. In VB it's optional: each property defaults to being mutable, but if you prefix it with Key it's immutable; only properties declared using Key count for equality and hash code generation. Personally I prefer C#'s approach.

CanWrite isn't always returned as true for properties in non-anonymous types - only for writable ones. Properties can be read-only, write-only, or read-write. For example:

public class Test
{
    // CanWrite will return false. CanRead will return true.
    public int ReadOnly { get { return 10; } }

    // CanWrite will return true. CanRead will return false.
    public int WriteOnly { set {} }

    // CanWrite will return true. CanRead will return true.
    public int ReadWrite { get { return 10; } set {} }
}
Jon Skeet
Hi,"isn't always"? Could you elaborate on that a little bit. Also I was expecting a set method to be generated too. Is the compiler smart enough to generate a setter only if it is used? - Thanks.
SharePoint Newbie
Also is it possible to know if a type is anonymous using reflection?
SharePoint Newbie
You *can't* use a setter with an anonymous type generated by C# - as I say, it's always immutable. For named types, whether or not you have a setter will depend on the declaration, as shown in the example. As for detecting whether a type was created by the compiler, there's another SO question about that somewhere. I'll try to find it.
Jon Skeet
Found it - see http://stackoverflow.com/questions/315146
Jon Skeet
+1  A: 

Properties of anonymous types cannot be assigned to, so reflection reports correctly.

If you were to look at the compiled IL, you'll notice that though the C# code looks like it is using a normal initializer, it is rewritten by the compiler to be a constructor call, which allows the properties to be non-writable outside of the class.

Lasse V. Karlsen
+1  A: 

How do I find out if a type is anonymous using reflection?

I think you can check if the type has the CompilerGenerated attribute

Thomas Levesque
+2  A: 

There is no reliable way to determine if a type is anonymous as the .NET 2.0 runtime didn't support anonymous types. Relying on the format of the "compiler generated name" is not a safe fix as this is likely to change with different versions of the compiler.

It sounds like you answered your own question regarding "How can I check whether the property can be written to" with the statements above: CanWrite is false and GetSetMethod(true) returns null. Those are two signs that you can't write to the property.

Since we're on the .NET 2.0 runtime, there is not an "IsAnonymous" property on System.Type so you don't really have any reliable method to identify an anonymous type.

Chris Patterson