473,472 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how to reference internal classes from generated code in C#?

C# allows code to be generated, but the generated code is in its own
assembly. Is there a way for that generated code to access internal
classes in the assembly that produced it? To have the generated code
pretend to be part of the assembly that produced it? Maybe some
compilation option? I want the generated code for efficiency, but I
don't want to pay for it by making all sorts of classes public that
would otherwise be internal.
Oct 13 '08 #1
4 1693
I think you need to rephrase the question, with a code example. I'm lost as
to what you want...

<bo*********@burtleburtle.netwrote in message
news:7f**********************************@e38g2000 prn.googlegroups.com...
C# allows code to be generated, but the generated code is in its own
assembly. Is there a way for that generated code to access internal
classes in the assembly that produced it? To have the generated code
pretend to be part of the assembly that produced it? Maybe some
compilation option? I want the generated code for efficiency, but I
don't want to pay for it by making all sorts of classes public that
would otherwise be internal.
Oct 14 '08 #2
On Mon, 13 Oct 2008 16:48:04 -0700, <bo*********@burtleburtle.netwrote:
C# allows code to be generated, but the generated code is in its own
assembly. Is there a way for that generated code to access internal
classes in the assembly that produced it? To have the generated code
pretend to be part of the assembly that produced it? Maybe some
compilation option? I want the generated code for efficiency, but I
don't want to pay for it by making all sorts of classes public that
would otherwise be internal.
First, you may want to be more specific about how having generated code
makes things more efficient. It's possible that your efficiency gain
isn't as great as you might think, or that there is an equivalent strategy
that can be used that achieves similar efficiency without run-time code
generation.

Beyond that, I admit I know next to nothing about classes like
CSharpCodeProvider and there may in fact be some approach you can take to
cause generated code to effectively be in a given assembly. But it seems
unlikely to me, because of the difficulty in ensuring that the code is
actually being compiled by the assembly it wants to be in. That is, if
you could specify an arbitrary assembly for compilation, some other code
could insert code into an assembly.

There might be some sort of trusted code rules that address that, but it
does seem like a potential code safety hole.

However, there are in fact strategies that can be used to get at internal
and even private members of an arbitrary class. The messiest is
reflection. Used carelessly, you can kill performance with reflection,
but if you know what you're doing you can use it to get at any member of
any type, including internal and private ones.

An approach that is probably more kosher is to use the
[InternalsVisibleTo] code attribute in the assembly that contains the
internals you want to make accessible, specifying the name of the assembly
you will be generating dynamically. See
http://msdn.microsoft.com/en-us/library/0tke9fxk.aspx for more details.

This last suggestion does of course assume that using a CodeProvider
implementation you can name the assembly you're compiling. That seems
like a reasonably expectation to me, but given my lack of experience with
it, I can't say for sure whether it'd work.

Pete
Oct 14 '08 #3
Never mind. Peter's interpretation seems to make sense now...

"Family Tree Mike" <Fa************@ThisOldHouse.comwrote in message
news:ej**************@TK2MSFTNGP04.phx.gbl...
>I think you need to rephrase the question, with a code example. I'm lost
as to what you want...

<bo*********@burtleburtle.netwrote in message
news:7f**********************************@e38g2000 prn.googlegroups.com...
>C# allows code to be generated, but the generated code is in its own
assembly. Is there a way for that generated code to access internal
classes in the assembly that produced it? To have the generated code
pretend to be part of the assembly that produced it? Maybe some
compilation option? I want the generated code for efficiency, but I
don't want to pay for it by making all sorts of classes public that
would otherwise be internal.
Oct 14 '08 #4
On Oct 13, 6:08*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Mon, 13 Oct 2008 16:48:04 -0700, <bob_jenk...@burtleburtle.netwrote:
C# allows code to be generated, but the generated code is in its own
assembly. *Is there a way for that generated code to access internal
classes in the assembly that produced it? *To have the generated code
pretend to be part of the assembly that produced it? *Maybe some
compilation option? *I want the generated code for efficiency, but I
don't want to pay for it by making all sorts of classes public that
would otherwise be internal.

First, you may want to be more specific about how having generated code *
makes things more efficient. *It's possible that your efficiency gain *
isn't as great as you might think, or that there is an equivalent strategy *
that can be used that achieves similar efficiency without run-time code *
generation.

Beyond that, I admit I know next to nothing about classes like *
CSharpCodeProvider and there may in fact be some approach you can take to*
cause generated code to effectively be in a given assembly. *But it seems *
unlikely to me, because of the difficulty in ensuring that the code is *
actually being compiled by the assembly it wants to be in. *That is, if*
you could specify an arbitrary assembly for compilation, some other code *
could insert code into an assembly.

There might be some sort of trusted code rules that address that, but it *
does seem like a potential code safety hole.

However, there are in fact strategies that can be used to get at internal*
and even private members of an arbitrary class. *The messiest is *
reflection. *Used carelessly, you can kill performance with reflection,*
but if you know what you're doing you can use it to get at any member of *
any type, including internal and private ones.

An approach that is probably more kosher is to use the *
[InternalsVisibleTo] code attribute in the assembly that contains the *
internals you want to make accessible, specifying the name of the assembly *
you will be generating dynamically. *See *http://msdn.microsoft.com/en-us/libr...ke9fxk.aspxfor more details.

This last suggestion does of course assume that using a CodeProvider *
implementation you can name the assembly you're compiling. *That seems *
like a reasonably expectation to me, but given my lack of experience with*
it, I can't say for sure whether it'd work.

Pete
Thanks for the pointer ...

If I read it right, normally friend assemblies are compiled and signed
with a private key locally, then sent to users. That works. But in
my case I want the users to run my code, which will generate code,
which would need to be compiled and signed. That requires shipping a
private key to users. That won't work. I'll live with some extra
classes being public, then.
Oct 14 '08 #5

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

Similar topics

9
by: qazmlp | last post by:
const has internal linkage in C++, but external linkage in C. Am I right ? But, linker reports multiply-defined error if the following header is included in multiple .cpp files. //...
3
by: MIGUEL | last post by:
Hi all, I'm quite lost with how adding web references to a project creates proxy classes. I've developed a web service with two classes inside and that contains three references to three...
0
by: Richard Gregory | last post by:
Hi, I have the wsdl below, for an Axis web service, and when I select Add Web Refernce in Visual Studio the proxy is missing a class representing the returnedElementsType (see reference.cs below...
0
by: craigd | last post by:
<shortversion>Why is the order of referenced DLLs important when compiling C# code. A DLL with 'internal' types appearing ahead of a DLL with the same 'public' types will cause the build 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
1
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...
0
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...
0
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,...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.