Instantiating an struct defined as DNVSRISKMDECORE!STRUCT1

  • Thread starter Thread starter Marek
  • Start date Start date
M

Marek

Hi
I am trying to call a method in a FORTRAN.NET assembly that requires a
structure parameter. Intellisense tells me that the structure name is
DNVSRISKMDECORE!STRUCT1 and this is confirmed by reflector. However in the
C#, declaring an object of type DNVSRISKMDECORE!STRUCT1 is rejected because
of the ! mark.

Please could anyone help?

Best regards

Marek
 
Marek said:
Hi
I am trying to call a method in a FORTRAN.NET assembly that requires a
structure parameter. Intellisense tells me that the structure name is
DNVSRISKMDECORE!STRUCT1 and this is confirmed by reflector. However in
the
C#, declaring an object of type DNVSRISKMDECORE!STRUCT1 is rejected
because
of the ! mark.

Please could anyone help?

From the C# specification: "Unicode escape sequences are permitted in
identifiers".

So use the following name:

DNVSRISKMDECORE\u0021STRUCT1
 
Hi Ben
Not sure if I understood or not, but using the name you suggested the
compiler complains:

error CS1056: Unexpected character '\u0021'

Best regards

Marek
 
Marek said:
I am trying to call a method in a FORTRAN.NET assembly that requires a
structure parameter. Intellisense tells me that the structure name is
DNVSRISKMDECORE!STRUCT1 and this is confirmed by reflector. However in the
C#, declaring an object of type DNVSRISKMDECORE!STRUCT1 is rejected because
of the ! mark.

Please could anyone help?

Unfortunately that name violates CLS rule 4. I'd expect quite a few
languages to have issues consuming it.

You may be forced to use reflection in order to access it from just C#.
If you have to use it a lot, you may be able to create your own
structure (probably using ILDASM/ILASM) which mirrors the existing
structure, and has conversion methods to/from the Fortran one. With
that in place, you'd never need to actually specify the name in C#
code. It's a really ugly solution though.
 
Punctuator characters are not allowed in identifiers (see ECMA-335 ch
8.5.1), so, ! is not allowed and as \U0021 is exactly the same a s !, the
compiler complains in both cases with the same error.
AFAIK, punctuations are not allowed in Fortran, so I doubt this is the real
class name you are referring to.
If Fortran.net does allow such names on public interfaces, then is violates
the above mentioned CLS rules.

Willy.
 
Hi Ben, Willy and Jon
Thanks for all your replies. I was worried that you were going to say that.
Our requirement is to call a method that uses this object so I can get away
with creating and using the object as follows:

Type struct1Type = assembly.GetType("DNVSRISKMDECORE!STRUCT1");
object struct1Instance = Activator.CreateInstance(struct1Type);

struct1Type.InvokeMember("iint1", BindingFlags.SetField, null,
struct1Instance, new object[] { -1 });
struct1Type.InvokeMember("iint2", BindingFlags.SetField, null,
struct1Instance, new object[] { -2 });
struct1Type.InvokeMember("ddouble1", BindingFlags.SetField,
null, struct1Instance, new object[] { -10.0 });
struct1Type.InvokeMember("ddouble2", BindingFlags.SetField,
null, struct1Instance, new object[] { -20.0 });

Thanks for your responses.

Best regards

Marek
 
Marek said:
Thanks for all your replies. I was worried that you were going to say that.
Our requirement is to call a method that uses this object so I can get away
with creating and using the object as follows:

Type struct1Type = assembly.GetType("DNVSRISKMDECORE!STRUCT1");
object struct1Instance = Activator.CreateInstance(struct1Type);

struct1Type.InvokeMember("iint1", BindingFlags.SetField, null,
struct1Instance, new object[] { -1 });
struct1Type.InvokeMember("iint2", BindingFlags.SetField, null,
struct1Instance, new object[] { -2 });
struct1Type.InvokeMember("ddouble1", BindingFlags.SetField,
null, struct1Instance, new object[] { -10.0 });
struct1Type.InvokeMember("ddouble2", BindingFlags.SetField,
null, struct1Instance, new object[] { -20.0 });

Thanks for your responses.

Do you only have to do this once? If so, that's a reasonable workaround
(horrible as it is that you have to do it). If you need to use it more
often, I'm happy to help you with the alternative idea about a
surrogate structure.
 
Hi Jon
Thanks for the offer. I did dabble with Reflector yesterday, which did show
how to create the object using:

[StructLayout(LayoutKind.Explicit, Size=0x18, Pack=8), DebugInfo(new byte[] {
0x69, 0x69, 110, 0x74, 0x31, 0, 0x20, 0, 0, 0, 4, 0, 0, 0, 0x10, 0,
0, 0, 4, 0, 0, 0, 0x69, 0x69, 110, 0x74, 50, 0, 0x20, 0, 0, 0,
4, 0, 0, 0, 20, 0, 0, 0, 4, 0, 0, 0, 100, 100, 0x6f, 0x75,
0x62, 0x6c, 0x65, 0x31, 0, 0x20, 0, 0, 0, 11, 0, 0, 0, 0, 0, 0,
0, 8, 0, 0, 0, 100, 100, 0x6f, 0x75, 0x62, 0x6c, 0x65, 50, 0, 0x20, 0,
0, 0, 11, 0, 0, 0, 8, 0, 0, 0, 8, 0, 0, 0, 0, 0
})]
public struct DNVSRISKMDECORE!STRUCT1
{
// Fields
[FieldOffset(0)]
public double ddouble1;
[FieldOffset(8)]
public double ddouble2;
[FieldOffset(0x10)]
public int iint1;
[FieldOffset(20)]
public int iint2;
}

The good news for me is that because of our application design, all the
methods and objects (lots of) are stored on a data dictionary and they are
built using the late invocation approach anyway.

Thanks again.

Best regards

Marek

Jon Skeet said:
Marek said:
Thanks for all your replies. I was worried that you were going to say that.
Our requirement is to call a method that uses this object so I can get away
with creating and using the object as follows:

Type struct1Type = assembly.GetType("DNVSRISKMDECORE!STRUCT1");
object struct1Instance = Activator.CreateInstance(struct1Type);

struct1Type.InvokeMember("iint1", BindingFlags.SetField, null,
struct1Instance, new object[] { -1 });
struct1Type.InvokeMember("iint2", BindingFlags.SetField, null,
struct1Instance, new object[] { -2 });
struct1Type.InvokeMember("ddouble1", BindingFlags.SetField,
null, struct1Instance, new object[] { -10.0 });
struct1Type.InvokeMember("ddouble2", BindingFlags.SetField,
null, struct1Instance, new object[] { -20.0 });

Thanks for your responses.

Do you only have to do this once? If so, that's a reasonable workaround
(horrible as it is that you have to do it). If you need to use it more
often, I'm happy to help you with the alternative idea about a
surrogate structure.
 
Back
Top