473,385 Members | 1,927 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,385 software developers and data experts.

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(TEST_SYMBOL_DEFINED)]
class ClassToBePublicInTestModeOnly
{
};

Does anyone have any ideas?

Thanks

Colin
May 4 '06 #1
4 1420
> 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(TEST_SYMBOL_DEFINED)]
class ClassToBePublicInTestModeOnly
{
};


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**********************@hotmail.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(TEST_SYMBOL_DEFINED)]
class ClassToBePublicInTestModeOnly
{
};


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**********************@hotmail.com
Remove only "_nos_pam"

May 4 '06 #3
Hi Colin,

"Colin Desmond" <Co**********@discussions.microsoft.com> wrote in message
news:93**********************************@microsof t.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(TEST_SYMBOL_DEFINED)]
class ClassToBePublicInTestModeOnly
{
};

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
InternalsVisibleToAttribute 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**********@discussions.microsoft.com> wrote in message
news:93**********************************@microsof t.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(TEST_SYMBOL_DEFINED)]
class ClassToBePublicInTestModeOnly
{
};

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
InternalsVisibleToAttribute 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
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...
12
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..,...
1
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,...
8
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...
19
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...
4
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...
1
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...
3
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...
4
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...

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.