Class visibility through custom attributes

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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.
(e-mail address removed)
Remove only "_nos_pam"
 
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 said:
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.
(e-mail address removed)
Remove only "_nos_pam"
 
Hi Colin,

Colin Desmond said:
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
 
Thank you Marcus,

That is exactly what I was after.

Colin.

Marcus Heege said:
Hi Colin,

Colin Desmond said:
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
 
Back
Top