473,776 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3826
Joerg Battermann <jb@joergbatter mann.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.co m>
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@joergbatter mann.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.co mwrote:
Joerg Battermann <j...@joergbatt ermann.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.co m>
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.Can Read && property.CanWri te))
{
throw new NotSupportedExc eption("Not an automatic property");
}

byte[] getter =
property.GetGet Method().GetMet hodBody().GetIL AsByteArray();
byte ldfld = (byte)(property .GetGetMethod() .IsStatic ?
OpCodes.Ldsfld : OpCodes.Ldfld). Value;
byte[] fieldToken = getter.SkipWhil e(b =b !=
ldfld).Skip(1). Take(4).ToArray ();
if (fieldToken.Len gth != 4)
{
throw new NotSupportedExc eption("Not an automatic property");
}
FieldInfo field =
property.Declar ingType.Module. ResolveField(Bi tConverter.ToIn t32(fieldToken,
0));
if (field == null)
{
throw new NotSupportedExc eption("Not an automatic property");
}

//Not sure about this: compilers don't strictly have to add this
attribute.
if (!field.IsDefin ed(typeof(Compi lerGeneratedAtt ribute), false))
{
throw new NotSupportedExc eption("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
2484
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 am wondering if there are any naming conventions around that are deemed suitable by the general C++ community. I have googled around and I can't find much - mostly long lists of hungarian-like prefixes which is not really what I'm after.
1
6547
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 so on? Any referrals to resources that discuss or document XML Naming Conventions? -- <%= Clinton Gallagher, "Twice the Results -- Half the Cost" Architectural & e-Business Consulting -- Software Development NET...
15
1381
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, so no problem there. In programming with VB6, I never wrote any program that used properties, and to date I still haven't ever used them in C#. What I really want to know is, when should I use them at all? I mean, we have variables (now referred...
5
7443
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
7149
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 convensions. The thing is that Ive never really get the full reference to check against. Ive seen a couple of articles, but there always seems to be a bit missing. I also always seem to run into conflicting convensions both in code samples themselves...
3
4352
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 so on? Any referrals to resources that discuss or document XML Naming Conventions? -- <%= Clinton Gallagher, "Twice the Results -- Half the Cost" Architectural & e-Business Consulting -- Software Development NET...
5
6173
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 wonder if I can use the same naming conventions as for VB6 ? If so, what about the names for the new objects in .NET. Kind regards rastaman
60
5063
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. consistently may make the code easier to understand for someone else, etc. Using "this." makes it immediately clear, for example, that the variable being referred to is a member of the same class and is not declared in the method as a local variable. ...
0
887
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 let you 'assume' a certain schema... Cheers and thanks, -jB
0
9464
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10292
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10122
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10061
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9923
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8954
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7471
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
3627
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2860
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.