The System.Runtime.InteropServices.MarshalAsAttribute can be applied to a field, parameter or return value within the source of the type definition. For example:
public static extern void PrintMessage( [MarshalAs(UnmanagedType.LPWSTR)]String msg);
Or for a field within a class:
class MsgText { [MarshalAs(UnmanagedType.LPWSTR)] String msg; }
And for a return value:
public static extern [MarshalAs(UnmanagedType.LPWSTR)] String GetMessage();
The System.Runtime.InteropServices.MarshalAsAttribute uses the System.Runtime.InteropServices.UnmanagedType enum (abbreviated UT throughout this document) to indicate the desired format of the unmanaged type. The above examples all indicate that the msg data should be marshaled as a buffer of Unicode characters (LPWSTR).