473,806 Members | 2,284 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Class visibility through custom attributes

Is it possible to write an attribute that changes the visibility of a class
(public/private) etc based on some pre-compiler define symbol?

We're writing some classes which will be called by an external (bespoke)
test harness, so we want then to be public in the assembly when we're
testing, and private in the class the rest of the time.

For example

[DefinePublic(TE ST_SYMBOL_DEFIN ED)]
class ClassToBePublic InTestModeOnly
{
};

Does anyone have any ideas?

Thanks

Colin
May 4 '06 #1
4 1436
> Is it possible to write an attribute that changes the visibility of a class
(public/private) etc based on some pre-compiler define symbol?

We're writing some classes which will be called by an external (bespoke)
test harness, so we want then to be public in the assembly when we're
testing, and private in the class the rest of the time.

For example

[DefinePublic(TE ST_SYMBOL_DEFIN ED)]
class ClassToBePublic InTestModeOnly
{
};


I do not know if you can do this property based. I somehow doubt it because
once the assembly is generated, I would expect visibility to be fixed.
If not, then anyone who knew your attributes could change visibility at will.

What you could do would be the following, though it fixes the visibility at
compile time.

#ifdef UNITTEST
#define VISIBILITY public
#else
#define VISIBILITY private
#endif

VISIBILITY:
ref class test
{
test()
{
}
};

--

Kind regards,
Bruno.
br************* *********@hotma il.com
Remove only "_nos_pam"
May 4 '06 #2
The behaviour you describe with the #ifdef is what I am trying to achieve
with the attribute. I expect the visibility to be set at compile time and not
changeable in the metadata.

I was just looking for a more visually pleasing way of achieving what the
ugly #ifdef code does.

Thanks anyway.

Colin.

"Bruno van Dooren" wrote:
Is it possible to write an attribute that changes the visibility of a class
(public/private) etc based on some pre-compiler define symbol?

We're writing some classes which will be called by an external (bespoke)
test harness, so we want then to be public in the assembly when we're
testing, and private in the class the rest of the time.

For example

[DefinePublic(TE ST_SYMBOL_DEFIN ED)]
class ClassToBePublic InTestModeOnly
{
};


I do not know if you can do this property based. I somehow doubt it because
once the assembly is generated, I would expect visibility to be fixed.
If not, then anyone who knew your attributes could change visibility at will.

What you could do would be the following, though it fixes the visibility at
compile time.

#ifdef UNITTEST
#define VISIBILITY public
#else
#define VISIBILITY private
#endif

VISIBILITY:
ref class test
{
test()
{
}
};

--

Kind regards,
Bruno.
br************* *********@hotma il.com
Remove only "_nos_pam"

May 4 '06 #3
Hi Colin,

"Colin Desmond" <Co**********@d iscussions.micr osoft.com> wrote in message
news:93******** *************** ***********@mic rosoft.com...
Is it possible to write an attribute that changes the visibility of a
class
(public/private) etc based on some pre-compiler define symbol?

We're writing some classes which will be called by an external (bespoke)
test harness, so we want then to be public in the assembly when we're
testing, and private in the class the rest of the time.

For example

[DefinePublic(TE ST_SYMBOL_DEFIN ED)]
class ClassToBePublic InTestModeOnly
{
};

Does anyone have any ideas?

Thanks

Colin


in .NET 1.0 and 1.1, there is nothing to achieve this.
in .NET 2.0, you should mark your class as private and apply the
InternalsVisibl eToAttribute at the assembly level to allow your testing code
assess to that member

Marcus
May 5 '06 #4
Thank you Marcus,

That is exactly what I was after.

Colin.

"Marcus Heege" wrote:
Hi Colin,

"Colin Desmond" <Co**********@d iscussions.micr osoft.com> wrote in message
news:93******** *************** ***********@mic rosoft.com...
Is it possible to write an attribute that changes the visibility of a
class
(public/private) etc based on some pre-compiler define symbol?

We're writing some classes which will be called by an external (bespoke)
test harness, so we want then to be public in the assembly when we're
testing, and private in the class the rest of the time.

For example

[DefinePublic(TE ST_SYMBOL_DEFIN ED)]
class ClassToBePublic InTestModeOnly
{
};

Does anyone have any ideas?

Thanks

Colin


in .NET 1.0 and 1.1, there is nothing to achieve this.
in .NET 2.0, you should mark your class as private and apply the
InternalsVisibl eToAttribute at the assembly level to allow your testing code
assess to that member

Marcus

May 5 '06 #5

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

Similar topics

50
6392
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong def foo(self, data): self.attr1=data self.attr2.append(data) The initialization of attr1 is obviously OK, all instances of Father redefine it in the method foo. But the initialization of attr2 is wrong
12
2022
by: Sunny | last post by:
Hi All, I have a serious issue regarding classes scope and visibility. In my application, i have a class name "TextFile", and also a few other classes like "TotalWords", "TotalLines" and etc.., which are suppose to describe the structure of my main TextFile class. Also i have created some custom collection classes, which only take items of the types, they are designed for. Now the problem is that I want my element classes, like...
1
2241
by: Mark | last post by:
Hi, Am trying to write custom validation attributes for public fields, properties, methods etc. using context-bound programming. It works fine for properties, methods, parameters etc. But, when intercepting a Set of a public field, there appears to be an underlying method set up; a FieldSetter, which take parameters type, fieldname and fieldvalue.
8
2409
by: reviswami78 | last post by:
Hi : I wanted to know if the following is possible in C# ( .NET Framework 1.x): I want to add an attribute to a class in a separate file i.e. in a file other than which the class itself is declared/defined. So: //File A.cs
19
4923
by: Jamey Shuemaker | last post by:
I'm in the process of expanding my knowledge and use of Class Modules. I've perused MSDN and this and other sites, and I'm pretty comfortable with my understanding of Class Modules with the exception of custom Collection Classes. Background: I'm developing an A2K .mdb to be deployed as an .mde at my current job-site. It has several custom controls which utilize custom classes to wrap built-in controls, and add additional functionality....
4
3326
by: Pedro Werneck | last post by:
Hi all I noticed something strange here while explaining decorators to someone. Not any real use code, but I think it's worth mentioning. When I access a class attribute, on a class with a custom metaclass with a __getattribute__ method, the method is used when acessing some attribute directly with the class object, but not when you do it from the instance.
1
2148
by: hardieca | last post by:
Hi! I decorate my unfinished classes and methods with a custom TODO attribute (as in things To Do). Using reflection, I am then able to parse through my classes and output all TODOs to a list I can examine to figure out what is left to be done in my application. The attribute is coded thusly: AttributeUsage((AttributeTargets.Class | AttributeTargets.Method),
3
1926
by: Michellevt | last post by:
Hi I am working on a project (for college) and wondered if anyone can help me with my problem. In the project we are not allowed to make use of any "style" attributes but "class" attributes instead. The following is the java script that i am using and i am having trouble in using a class instead of a style tag because simply replacing the style=\"position:absolute;\" tag with class=\"posiAbs\" where in the external css ".posiAbs...
4
2357
by: Ty | last post by:
Hello all, I am creating a website in VS 2008 VB.net. On one of my pages I am using the Table control to make a type of calendar a IN/OUT board. The problem I found after I wrote all the code to create the table was that there is not Cell Click event. So I did some looking and asked around and got this C# (see below) version of a class that someone built to create a clickable cell.
0
9596
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
10617
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
10364
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...
0
10109
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...
1
7649
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...
0
5545
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3849
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
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.