Skip to content

Commit 137accf

Browse files
Provide valid C# example and remove C++/CLI function definition.
1 parent 40f4180 commit 137accf

File tree

2 files changed

+17
-5
lines changed
  • samples/snippets/csharp/VS_Snippets_CLR_System/system.runtime.interopservices.icustommarshaler/cs
  • xml/System.Runtime.InteropServices

2 files changed

+17
-5
lines changed

samples/snippets/csharp/VS_Snippets_CLR_System/system.runtime.interopservices.icustommarshaler/cs/source.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,27 @@ interface IUserData
3838
{
3939
void DoSomeStuff(
4040
[MarshalAs(UnmanagedType.CustomMarshaler,
41-
MarshalType="MyCompany.NewOldMarshaler")]
41+
MarshalType="NewOldMarshaler")]
4242
INew pINew
4343
);
4444
}
4545
// </Snippet5>
4646
}
4747

48+
// <Snippet6>
49+
public NewOldMarshaler : ICustomMarshaler
50+
{
51+
public static ICustomMarshaler GetInstance(string pstrCookie)
52+
=> return new NewOldMarshaler();
53+
54+
public Object MarshalNativeToManaged( IntPtr pNativeData ) => throw new NotImplementedException();
55+
public IntPtr MarshalManagedToNative( Object ManagedObj ) => throw new NotImplementedException();
56+
public void CleanUpNativeData( IntPtr pNativeData ) => throw new NotImplementedException();
57+
public void CleanUpManagedData( Object ManagedObj ) => throw new NotImplementedException();
58+
public int GetNativeDataSize() => throw new NotImplementedException();
59+
}
60+
// </Snippet6>
61+
4862
class StubClass
4963
{
5064
public static void Main() {}

xml/System.Runtime.InteropServices/ICustomMarshaler.xml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,9 @@ const IID IID_IOld = {0x9B2BAADD,0x0705,0x11D3,{0xA0,0xCD,0x00,0xC0,0x4F,0xA3,0x
124124
Returns the size of the unmanaged data to be marshaled.
125125
126126
## Implementing the GetInstance Method
127-
In addition to implementing the <xref:System.Runtime.InteropServices.ICustomMarshaler> interface, custom marshalers must implement a `static` method called `GetInstance` that accepts a <xref:System.String> as a parameter and has a return type of <xref:System.Runtime.InteropServices.ICustomMarshaler>. This `static` method is called by the common language runtime's COM interop layer to instantiate an instance of the custom marshaler. The string that is passed to `GetInstance` is a cookie that the method can use to customize the returned custom marshaler.
127+
In addition to implementing the <xref:System.Runtime.InteropServices.ICustomMarshaler> interface, custom marshalers must implement a `static` method called `GetInstance` that accepts a <xref:System.String> as a parameter and has a return type of <xref:System.Runtime.InteropServices.ICustomMarshaler>. This `static` method is called by the common language runtime's COM interop layer to instantiate an instance of the custom marshaler. The string that is passed to `GetInstance` is a cookie that the method can use to customize the returned custom marshaler. A minimal, but complete, <xref:System.Runtime.InteropServices.ICustomMarshaler> implementation in the form of the `NewOldMarshaler` type is below.
128128
129-
```
130-
static ICustomMarshaler *GetInstance(String *pstrCookie);
131-
```
129+
[!code-csharp[System.Runtime.InteropServices.ICustomMarshaler#6](~/samples/snippets/csharp/VS_Snippets_CLR_System/system.runtime.interopservices.icustommarshaler/cs/source.cs#6)]
132130
133131
## Applying MarshalAsAttribute
134132
To use a custom marshaler, you must apply the <xref:System.Runtime.InteropServices.MarshalAsAttribute> attribute to the parameter or field that is being marshaled.

0 commit comments

Comments
 (0)