public void Clear() { // We do not need to call OLE32's VariantClear for primitive types or ByRefs // to safe ourselves the cost of interop transition. // ByRef indicates the memory is not owned by the VARIANT itself while // primitive types do not have any resources to free up. // Hence, only safearrays, BSTRs, interfaces and user types are // handled differently. VarEnum vt = VariantType; if ((vt & VarEnum.VT_BYREF) != 0) { VariantType = VarEnum.VT_EMPTY; } else if ( ((vt & VarEnum.VT_ARRAY) != 0) || ((vt) == VarEnum.VT_BSTR) || ((vt) == VarEnum.VT_UNKNOWN) || ((vt) == VarEnum.VT_DISPATCH) || ((vt) == VarEnum.VT_RECORD) ) { IntPtr variantPtr = UnsafeMethods.ConvertVariantByrefToPtr(ref this); NativeMethods.VariantClear(variantPtr); Debug.Assert(IsEmpty); } else { VariantType = VarEnum.VT_EMPTY; } }
internal static void InitVariantForObject(object obj, ref Variant variant) { Debug.Assert(obj != null); // GetNativeVariantForObject is very expensive for values that marshal as VT_DISPATCH // also is is extremely common scenario when object at hand is an RCW. // Therefore we are going to test for IDispatch before defaulting to GetNativeVariantForObject. IDispatch disp = obj as IDispatch; if (disp != null) { variant.AsDispatch = obj; return; } System.Runtime.InteropServices.Marshal.GetNativeVariantForObject(obj, UnsafeMethods.ConvertVariantByrefToPtr(ref variant)); }
public void SetAsByrefVariant(ref Variant value) { Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise VariantType = (VarEnum.VT_VARIANT | VarEnum.VT_BYREF); _typeUnion._unionTypes._byref = UnsafeMethods.ConvertVariantByrefToPtr(ref value); }
public static object GetObjectForVariant(Variant variant) { IntPtr ptr = UnsafeMethods.ConvertVariantByrefToPtr(ref variant); return(System.Runtime.InteropServices.Marshal.GetObjectForNativeVariant(ptr)); }