public AssemblyName(string assemblyName) { if (assemblyName == null) { throw new ArgumentNullException(nameof(assemblyName)); } if (assemblyName.Length == 0 || assemblyName[0] == '\0') { throw new ArgumentException(SR.Format_StringZeroLength); } using (SafeStringMarshal name = RuntimeMarshal.MarshalString(assemblyName)) { // TODO: Should use CoreRT AssemblyNameParser if (!ParseAssemblyName(name.Value, out MonoAssemblyName nativeName, out bool isVersionDefined, out bool isTokenDefined)) { throw new FileLoadException("The assembly name is invalid."); } try { unsafe { FillName(&nativeName, null, isVersionDefined, false, isTokenDefined); } } finally { RuntimeMarshal.FreeAssemblyName(ref nativeName, false); } } }
void Log(string message) { const int buffer_size = 512; unsafe { if (Encoding.UTF8.GetByteCount(message) < buffer_size) { try { fixed(char *b_message = message) { byte *buffer = stackalloc byte[buffer_size]; int written = Encoding.UTF8.GetBytes(b_message, message.Length, buffer, buffer_size - 1); buffer [written] = (byte)'\0'; Log(buffer); } return; } catch (ArgumentException) { /* It might be due to a failure to encode a character, or due to a smaller than necessary buffer. In the * secode case, we want to fallback to simply allocating on the heap. */ } } using (SafeStringMarshal str = new SafeStringMarshal(message)) { Log((byte *)str.Value); } } }
private IntPtr InitializeAssemblyLoadContext(IntPtr thisHandlePtr, bool representsTPALoadContext, bool isCollectible) { using (SafeStringMarshal handle = RuntimeMarshal.MarshalString(Name)) { return(InternalInitializeNativeALC(thisHandlePtr, handle.Value, representsTPALoadContext, isCollectible)); } }
private static string InternalGetEnvironmentVariable(string name) { using (SafeStringMarshal handle = RuntimeMarshal.MarshalString(name)) { return(internalGetEnvironmentVariable_native(handle.Value)); } }