473,287 Members | 3,181 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Automatic Properties Backing Field Naming Conventions?

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
Aug 11 '08 #1
4 3791
Joerg Battermann <jb@joergbattermann.comwrote:
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/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 11 '08 #2
On Mon, 11 Aug 2008 10:58:07 -0700, Joerg Battermann
<jb@joergbattermann.comwrote:
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
Aug 11 '08 #3
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.comwrote:
Joerg Battermann <j...@joergbattermann.comwrote:
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/~skeet*
Blog:http://www.msmvps.com/jon.skeet
C# in Depth:http://csharpindepth.com
Aug 11 '08 #4
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().GetILAsByt eArray();
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(BitConv erter.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(CompilerGeneratedAttribut e), false))
{
throw new NotSupportedException("Not an automatic property");
}
return field;
}

Alun Harford
Aug 12 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: cmiddlebrook | last post by:
Hi there, I keep finding myself getting inconsistent with naming conventions for things like member variables, class names etc and I just want to find something that suits me and stick to it. I...
1
by: clintonG | last post by:
Does the use of DTD, XML Schema and similar constructs adopt the use of C# naming conventions? If so how do I make the distinction of how to apply C# conventions with XML elements, attributes and...
15
by: Gary Morris | last post by:
Hello all, OK, first of all I have known about properties since VB6, which I have and have used extensively. It seems that property get and set are basically the same concept in C# and VB.NET,...
5
by: Abhishek Srivastava | last post by:
Hello All, There is a particular feature which I want to have in visual studio .net suppose I create a class ClassA { private string name; private int age;
4
by: Mark Broadbent | last post by:
stupid question time again to most of you experts but this is something that continually bothers me. I am trying to get into the habit of naming variables and controls in an assembly as per...
3
by: clintonG | last post by:
Does the use of DTD, XML Schema and similar constructs adopt the use of C# naming conventions? If so how do I make the distinction of how to apply C# conventions with XML elements, attributes and...
5
by: rastaman | last post by:
Hi all, I know of the existence of Object Naming Conventions for Visual Basic 6. Now I'm reading some books about VB .NET, but the names used for the objects are button1, picturebox1, etc. I...
60
by: Dave | last post by:
I'm never quite sure whether to use "this." or not when referring to fields or properties in the same class. It obviously works just fine without it but sometimes I wonder if using this....
0
by: =?ISO-8859-1?Q?J=F6rg_Battermann?= | last post by:
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.