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

Regarding the access private strucuture members in C#

Hi,

I am doing the unit testing in C#, Where in i have to access the private structure members from different assembly (to testing assembly). I am unable to access the private structure members even through reflection. Can any one help me with solution?

Thanks in advance
Jane smith
Mar 18 '10 #1
4 1608
jkmyoung
2,057 Expert 2GB
Have you considered using public accessors in the code? Eg:
http://www.csharp-station.com/Tutorials/Lesson10.aspx
Mar 18 '10 #2
Hi,

Thanks for your reply. Let me clarify to some more extent. Here i want to access a private structure members using reflection and i dont want to instrument the source code.
Mar 19 '10 #3
jkmyoung
2,057 Expert 2GB
To clarify, are you looking just for the member name and type, or the actual data for the member? If data, then it's intentionally forbidden, hence the private modifier. If member name and type, then assuming it's working for your public members, could you post the code?
Mar 19 '10 #4
Sure, I am pasting the code below where in the I have to set the value for "ELF_Header->e_shentsize.Byte0" which is present in Reader.cs file into my TestReader.cs (test assembly). I am not able to access the structure member even through reflection.

Thanks in advance
Jane


Reader.cs
namespace Configurator
{
unsafe public partial class Reader
{
private Elf32_Ehdr* ELF_Header;
unsafe private String Get_SecName(int NameOffset)
{

int Position =
ELF_Header->e_shentsize.Byte0 + ELF_Header->e_shentsize.Byte1 * 256;
Position =
Position*(ELF_Header->e_shstrndx.Byte0 + ELF_Header->e_shstrndx.Byte1 * 256);
Position =
Position + ELF_Header->e_shoff.Byte0 + ELF_Header->e_shoff.Byte1 * 256 + ELF_Header->e_shoff.Byte2 * 65536 + ELF_Header->e_shoff.Byte3 * 16777216;
fixed (Byte* pTemp = &data[Position])
{
…….
}
String name = System.Text.Encoding.ASCII.GetString(data, Position + NameOffset, 10);
return name;
}
}
}
Definition.cs
namespace Configurator
{

//header
[StructLayout(LayoutKind.Sequential, Pack = 8)]
private unsafe struct Elf32_Ehdr
{
public unsafe Elf32_Off e_shoff;
public unsafe Elf32_Half e_shentsize;
public unsafe Elf32_Half e_shstrndx;
};
private unsafe struct Elf32_Off
{
public Byte Byte3;
public Byte Byte2;
public Byte Byte1;
public Byte Byte0;
};
private unsafe struct Elf32_Half
{
public Byte Byte1;
public Byte Byte0;
};
}
Mar 22 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Wolfgang Jeltsch | last post by:
Hello, I want to write a list class with an iterator class as an inner class. The iterator class must have access to certain private members of the list class in order to do its job. Here is a...
2
by: diadia | last post by:
when i write two classes as follow . compiler tell me that i can't access private number from other class but if types of two classes are the same it don't have error message ? why? class...
8
by: CoolPint | last post by:
I read in books that nested class cannot access private members of nesting class and vice versa unless they are made friends. Somehow, my compiler is letting my nested class member functions access...
3
by: darkstorm | last post by:
I have a doubt regarding inheritance involving templates Consider this: ///////////////////////////////////// template<typename T> class A { private: T m_a;
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
3
by: Chua Wen Ching | last post by:
Hi there, I had seen examples for classes, but i had no idea how to implement the same thing in struct. I am quite mix up! Which one is correct? Scenario: WForm.cs - the one that calls...
10
by: Abelardo Vacca | last post by:
Hi, The title sums up the question pretty much. I would like to access all private members of a class including the private members of its base classes.( I already have the ReflectionPermission )...
5
by: Lyle Fairfield | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/callnetfrcom.asp The Joy of Interoperability Sometimes a revolution in programming forces you to abandon all...
1
by: vidhyamariappan | last post by:
hai, i have to write a common function in C to write the strucuture values in a text file.this function must be comaptible to write any strucuture given as input.i mean that function can get any...
15
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I met with a strange issue that derived class function can not access base class's protected member. Do you know why? Here is the error message and code. error C2248:...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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...
0
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...

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.