private Array GetVector <T>() { int count = PropVariantNativeMethods.PropVariantGetElementCount(this); if (count <= 0) { return(null); } lock (_padlock) { if (_vectorActions == null) { _vectorActions = GenerateVectorActions(); } } Action <PropVariant, Array, uint> action; if (!_vectorActions.TryGetValue(typeof(T), out action)) { throw new InvalidCastException("Cannot be cast to unsupported type."); } Array array = new T[count]; for (uint i = 0; i < count; i++) { action(this, array, i); } return(array); }
// A string requires a special case because it's not a struct or value type private string[] GetStringVector() { int count = PropVariantNativeMethods.PropVariantGetElementCount(ref this); if (count <= 0) { return(null); } string[] strArr = new string[count]; for (uint i = 0; i < count; i++) { PropVariantNativeMethods.PropVariantGetStringElem(ref this, i, out strArr[i]); } return(strArr); }
private Array GetVector <T>() where T : struct { int count = PropVariantNativeMethods.PropVariantGetElementCount(ref this); if (count <= 0) { return(null); } Array arr = new T[count]; for (uint i = 0; i < count; i++) { if (typeof(T) == typeof(Int16)) { short val; PropVariantNativeMethods.PropVariantGetInt16Elem(ref this, i, out val); arr.SetValue(val, i); } else if (typeof(T) == typeof(UInt16)) { ushort val; PropVariantNativeMethods.PropVariantGetUInt16Elem(ref this, i, out val); arr.SetValue(val, i); } else if (typeof(T) == typeof(Int32)) { int val; PropVariantNativeMethods.PropVariantGetInt32Elem(ref this, i, out val); arr.SetValue(val, i); } else if (typeof(T) == typeof(UInt32)) { uint val; PropVariantNativeMethods.PropVariantGetUInt32Elem(ref this, i, out val); arr.SetValue(val, i); } else if (typeof(T) == typeof(Int64)) { long val; PropVariantNativeMethods.PropVariantGetInt64Elem(ref this, i, out val); arr.SetValue(val, i); } else if (typeof(T) == typeof(UInt64)) { ulong val; PropVariantNativeMethods.PropVariantGetUInt64Elem(ref this, i, out val); arr.SetValue(val, i); } else if (typeof(T) == typeof(DateTime)) { System.Runtime.InteropServices.ComTypes.FILETIME val; PropVariantNativeMethods.PropVariantGetFileTimeElem(ref this, i, out val); long fileTime = FileTimeToDateTime(ref val); arr.SetValue(DateTime.FromFileTime(fileTime), i); } else if (typeof(T) == typeof(Boolean)) { bool val; PropVariantNativeMethods.PropVariantGetBooleanElem(ref this, i, out val); arr.SetValue(val, i); } else if (typeof(T) == typeof(Double)) { double val; PropVariantNativeMethods.PropVariantGetDoubleElem(ref this, i, out val); arr.SetValue(val, i); } else if (typeof(T) == typeof(String)) { string val; PropVariantNativeMethods.PropVariantGetStringElem(ref this, i, out val); arr.SetValue(val, i); } } return(arr); }