Enforce declaration of values (C# 2.0)

M

Michael Schöller

Hi I have some "unusual" problem ^^

Is there an way to enforce the declaration of values?
If the answer is no use propertys can anyone tell me how to access a
construct like this over properties.
Access should be easy like
....some code
Memory m;
....some code
byte v = m.complete[22];
....some code

[StructLayout(LayoutKind.Explicit, Size = 20)]
public unsafe struct Memory
{
[FieldOffset(0)]
public fixed byte complete[20];
[FieldOffset(0)]
public fixed sbyte signedcompete[20];
}

The topic for the solution is "Get fastes possible access in C#"

Thanks
Michael
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

I did not understand nothing from your post, would you mind to elaborate
further?
 
M

Michael Schöller

What I try to do is create an replaceable library that contains the struct
"Memory".
I want to acces that sruct over an interface so that someone who figures out
an faster way (performance is important) can write an alternative library
based on the interface.
The programm itself emulates an 8bit microprocessor with an reduced
instruction set (64 OP codes). The programm should be as fast as possible to
keep testtime short. To use C# 2.0 for the programm was choosen by
management so switching to C++ is not an option.
The struct itself repesents the memory of the microprocessor, some of the OP
codes uses math operations that expect unsigned bytes an some expect signed
bytes. There are also some special memory locations that should be
accessable with special names like the Flagregister that is located in the
last byte of the memory (already found a solution that work with interfaces
over properties, unfortunaly this soloution seems not to work for arrays).
As you see the struct is very special in that way that the array are share
the same memory one is sbyte the other byte what grants fast access both
ways.
Also the example I had given is very simplyfied the original Memory-object
also has special adress ranges that would look like
[StructLayout(LayoutKind.Explicit, Size = 20)]
public unsafe struct Memory
{
[FieldOffset(0)]
public fixed byte complete[20];
[FieldOffset(0)]
public fixed sbyte signedcompete[20];
[FieldOffset(5)]
public fixed byte pathcontrol[5];
}
If I would use properties to create the same effect I would have to copy the
datas in the set and get statement while converting them if needed The
property of pathcontrol the normal way would be a simple
complete.CopyTo(...) and vice versa but thats the slowest possible solution.
The example i had given is much faster.

Now there should be the posibillity to excange the library that contains
this special struct, lets say with an code that do not need unsafe and fixed
statements without changing the rest of the application. Normaly I would
define an interface that enables access to the memory object but I have no
idea how to access the arraysover an interface by using properties without
copy the arrays to an temorary byte[] object.
What I am looking for is an solution that may use properties or whatever,
but do not need to make temporary copys of the arrays in the struct.

At first I was thinking about an construct like (I know that this code is
not working it's just "dreamcode [<-to good to be true]")
StructLayout(LayoutKind.Explicit, Size = 20)]
public unsafe struct Memory : IMOMCMemory
{
[FieldOffset(0)]
private fixed byte complete[20];

public byte CompleteMemory[int index] //Interfaceimplementation.
{
get{ return complete[index]; }
set { complete[index] = value; }
}
}
Something like that, so that I can get an easy access to the arrays.
Indexers would be an idea but I would need more than one (signed/unsigned
access to the whole memory and parts of it) and since there would only be
one parameter "int index" thery would be not well definded.

I hope that makes it clear

Michael

Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

I did not understand nothing from your post, would you mind to elaborate
further?


--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Michael Schöller said:
Hi I have some "unusual" problem ^^

Is there an way to enforce the declaration of values?
If the answer is no use propertys can anyone tell me how to access a
construct like this over properties.
Access should be easy like
...some code
Memory m;
...some code
byte v = m.complete[22];
...some code

[StructLayout(LayoutKind.Explicit, Size = 20)]
public unsafe struct Memory
{
[FieldOffset(0)]
public fixed byte complete[20];
[FieldOffset(0)]
public fixed sbyte signedcompete[20];
}

The topic for the solution is "Get fastes possible access in C#"

Thanks
Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top