[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework

Automatic Properties Backing Field Naming Conventions?

Jörg Battermann

8/11/2008 5:58:00 PM

Hello there,

does anyone know the precise naming conventions used for internal
backing fields for automatic properties? Something official besides
looking at the compiled assemblies that might let you 'assume' a
certain schema...

Cheers and thanks,
-jB
4 Answers

Jon Skeet

8/11/2008 6:13:00 PM

0

Joerg Battermann <jb@joergbattermann.com> wrote:
> does anyone know the precise naming conventions used for internal
> backing fields for automatic properties? Something official besides
> looking at the compiled assemblies that might let you 'assume' a
> certain schema...

I wouldn't make any assumptions. If you need direct access to the
fields, don't use automatic properties. The naming scheme is compiler-
dependent and unspecified; it could change between compiler versions.

--
Jon Skeet - <skeet@pobox.com>
Web site: http://www.pobox....
Blog: http://www.msmvps.com...
C# in Depth: http://csharpi...

Peter Duniho

8/11/2008 6:19:00 PM

0

On Mon, 11 Aug 2008 10:58:07 -0700, Joerg Battermann
<jb@joergbattermann.com> wrote:

> does anyone know the precise naming conventions used for internal
> backing fields for automatic properties? Something official besides
> looking at the compiled assemblies that might let you 'assume' a
> certain schema...

Surely this is undocumented for a reason.

I think the best you can do is inspect what the compiler generates, and
you absolutely definitely no way should ever actually rely on that
information. The compiler could change it's behavior any time.

For what it's worth, if you have control over the code implementing the
property, you can just not use the automatically implemented property and
name the field whatever you want. Automatically implemented properties
don't add _that_ much. And if you don't have control over the code
implementing the property, I don't think you even have a reliable way to
distinguish automatically implemented properties from regular ones.

Pete

Jörg Battermann

8/11/2008 7:53:00 PM

0

Argh - ok that's bad (for me). Basically I have an oodbms attached to
some of my classes.. and adding indexes relies in knowing the field
names. Bad... but oh well.

Thanks Jon & Peter!

On Aug 11, 8:13 pm, Jon Skeet [C# MVP] <sk...@pobox.com> wrote:
> Joerg Battermann <j...@joergbattermann.com> wrote:
> > does anyone know the precise naming conventions used for internal
> > backing fields for automatic properties? Something official besides
> > looking at the compiled assemblies that might let you 'assume' a
> > certain schema...
>
> I wouldn't make any assumptions. If you need direct access to the
> fields, don't use automatic properties. The naming scheme is compiler-
> dependent and unspecified; it could change between compiler versions.
>
> --
> Jon Skeet - <sk...@pobox.com>
> Web site:http://www.pobox.com/~s...
> Blog:http://www.msmvps.com...
> C# in Depth:http://csharpi...

Alun Harford

8/12/2008 7:28:00 PM

0

Joerg Battermann wrote:
> Argh - ok that's bad (for me). Basically I have an oodbms attached to
> some of my classes.. and adding indexes relies in knowing the field
> names. Bad... but oh well.

Hmm... well... the following code will give you the field for
automatically generated properties on all but the most crazy compilers,
where it could potentially do odd things (for example, if the automatic
property's getter strangly called a method before accessing the field
and that method happened to contain 0x7B in its token).

Cecil would be useful here and could parse the IL instead of guessing
that the first instance of 0x7B is the ldfld instruction so the token
will come next.

Having said that, it does work for any reasonable compiler and I wanted
to keep the code simple:

private static FieldInfo GetBackingField(PropertyInfo property)
{
if (!(property.CanRead && property.CanWrite))
{
throw new NotSupportedException("Not an automatic property");
}

byte[] getter =
property.GetGetMethod().GetMethodBody().GetILAsByteArray();
byte ldfld = (byte)(property.GetGetMethod().IsStatic ?
OpCodes.Ldsfld : OpCodes.Ldfld).Value;
byte[] fieldToken = getter.SkipWhile(b => b !=
ldfld).Skip(1).Take(4).ToArray();
if (fieldToken.Length != 4)
{
throw new NotSupportedException("Not an automatic property");
}
FieldInfo field =
property.DeclaringType.Module.ResolveField(BitConverter.ToInt32(fieldToken,
0));
if (field == null)
{
throw new NotSupportedException("Not an automatic property");
}

//Not sure about this: compilers don't strictly have to add this
attribute.
if (!field.IsDefined(typeof(CompilerGeneratedAttribute), false))
{
throw new NotSupportedException("Not an automatic property");
}
return field;
}

Alun Harford