private static void SetProperty(this IAssemblyName name, AssemblyNamePropertyId propertyId, object value) { int allocatedBytes = 0; var ptr = value == null ? IntPtr.Zero : value.ToPointer(out allocatedBytes); try { // First clear the property Marshal.ThrowExceptionForHR(name.SetProperty(propertyId, IntPtr.Zero, 0)); // Now set the property Marshal.ThrowExceptionForHR(name.SetProperty(propertyId, ptr, (uint)allocatedBytes)); } finally { Marshal.FreeHGlobal(ptr); } }
private static T GetProperty <T>(this IAssemblyName name, AssemblyNamePropertyId propertyId) { uint bufferSize = 512; var bufferPointer = Marshal.AllocHGlobal((int)bufferSize); try { Marshal.ThrowExceptionForHR(name.GetProperty(propertyId, bufferPointer, ref bufferSize)); return(bufferSize > 0 // IAssemblyName.GetProperty() will always return a bufferSize greater than 0 ? bufferPointer.Read <T>(bufferSize) : default(T)); } finally { Marshal.FreeHGlobal(bufferPointer); } }