What does it mean in terms of C#?

1.1k views Asked by At

I found the following compiler-generated code using DotPeek analyzing one of .NET assemblies:

[UnsafeValueType, NativeCppClass]
[StructLayout(LayoutKind.Sequential, Size = 16)]
internal struct DWriteTextAnalysisNode<MS::Internal::Text::TextInterface::Native::IDWriteNumberSubstitution *>
{
}

I am wondering what it means? How to rewrite this in C#? Especially this part:

<MS::Internal::Text::TextInterface::Native::IDWriteNumberSubstitution *>
1

There are 1 answers

0
Hans Passant On BEST ANSWER

You found this back in PresentationCore.dll. You are looking at code that was written in C++/CLI, not C#. It is one of the three major Microsoft .NET languages, beyond C# and VB.NET. Definitely the ugly step daughter, C++/CLI reason for being is its unparalleled ability to interop with native code written in C or C++.

Which gets lots of use in PresentationCore, System.Data as well, it contains heavy native code interop to take advantage of existing Microsoft apis. In this case it is DirectWrite, a text rendering api. It is a pretty complex native api that's well beyond the ability of standard pinvoke to interop with. C++/CLI supports using the native COM-style interfaces directly.

One aspect of C++/CLI is that the native C++ interfaces and classes can bleed into the assembly metadata. It is a bit of a flaw, it doesn't have a great way to suppress visibility of the native types. Only the opposite, making them visible with #pragma make_public. The C++/CLI compiler must generate a metadata definition for the type that's compatible with assembly metadata. Since these are native types under the hood, the closest reasonable match is a value type. It is entirely opaque, you don't see the members of the type. Just a blob, pretty similar to fixed size buffer types in C#.

So this is just an artifact. There's no sensible way to do the same thing in C#, nor is there a reason to.