From ee72119e36ec1db4196081f3f76cb2b72676d261 Mon Sep 17 00:00:00 2001 From: David Hall Date: Sat, 17 Aug 2019 14:42:49 -0600 Subject: [PATCH] MAJOR BREAKING CHANGE: Moved all extension methods for System.Object to Vanara.Extensions.Reflection namespace to avoid Intellisense overloads with methods that will be rarely used. --- Core/Extensions/ReflectionExtensions.cs | 257 +++++++++++---------- PInvoke/ComCtl32/CommCtrl.Header.cs | 10 +- PInvoke/ComCtl32/CommCtrl.ListView.cs | 6 +- PInvoke/Kernel32/ErrHandlingApi.cs | 5 +- PInvoke/NTDll/NTDll.cs | 9 +- Security/ActiveDirectoryExtension.cs | 1 + .../Core/Extensions/ReflectionExtensionsTests.cs | 3 +- WIndows.Forms/Components/MRUManager.cs | 5 +- .../Dialogs/AccessControlEditor/SecuredObject.cs | 1 + WIndows.Forms/Extensions/ControlExtensionGDI.cs | 1 + Windows.Shell/BindContext.cs | 1 + Windows.Shell/ShellExtensions/ComClassFactory.cs | 2 +- 12 files changed, 163 insertions(+), 138 deletions(-) diff --git a/Core/Extensions/ReflectionExtensions.cs b/Core/Extensions/ReflectionExtensions.cs index aea180aa..fd0e2442 100644 --- a/Core/Extensions/ReflectionExtensions.cs +++ b/Core/Extensions/ReflectionExtensions.cs @@ -4,72 +4,12 @@ using System.Linq; using System.Reflection; using System.Reflection.Emit; -namespace Vanara.Extensions +namespace Vanara.Extensions.Reflection { - /// Extensions related to System.Reflection + /// Extensions for related to System.Reflection public static class ReflectionExtensions { private const BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.IgnoreCase; - private const BindingFlags staticBindingFlags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.IgnoreCase; - - /// For a structure, gets either the result of the static Create method or the default. - /// The structure's type. - /// The result of the static Create method or the default. - public static T CreateOrDefault() where T : struct - { - var mi = typeof(T).GetMethod("Create", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static, null, Type.EmptyTypes, null); - return mi == null ? (default) : (T)mi.Invoke(null, null); - } - - /// Gets all loaded types in the . - /// The application domain. - /// All loaded types. - public static IEnumerable GetAllTypes(this AppDomain appDomain) => appDomain.GetAssemblies().SelectMany(a => a.GetTypes()); - - /// Returns an array of custom attributes applied to this member and identified by . - /// The type of attribute to search for. Only attributes that are assignable to this type are returned. - /// An object derived from the MemberInfo class that describes a constructor, event, field, method, or property member of a class. - /// true to search this member's inheritance chain to find the attributes; otherwise, false. This parameter is ignored for properties and events. - /// An optional predicate to refine the results. - /// - public static IEnumerable GetCustomAttributes(this MemberInfo element, bool inherit = false, Func predicate = null) where TAttr : Attribute => - element.GetCustomAttributes(typeof(TAttr), inherit).Cast().Where(predicate ?? (a => true)); - - /// Returns an array of custom attributes applied to this member and identified by . - /// The type of attribute to search for. Only attributes that are assignable to this type are returned. - /// The type of the to examine. - /// true to search this member's inheritance chain to find the attributes; otherwise, false. This parameter is ignored for properties and events. - /// An optional predicate to refine the results. - /// - public static IEnumerable GetCustomAttributes(this Type type, bool inherit = false, Func predicate = null) where TAttr : Attribute => - type.GetCustomAttributes(typeof(TAttr), inherit).Cast().Where(predicate ?? (a => true)); - - /// Finds the type of the element of a type. Returns null if this type does not enumerate. - /// The type to check. - /// The element type, if found; otherwise, . - public static Type FindElementType(this Type type) - { - if (type.IsArray) - return type.GetElementType(); - - // type is IEnumerable; - if (ImplIEnumT(type)) - return type.GetGenericArguments().First(); - - // type implements/extends IEnumerable; - var enumType = type.GetInterfaces().Where(ImplIEnumT).Select(t => t.GetGenericArguments().First()).FirstOrDefault(); - if (enumType != null) - return enumType; - - // type is IEnumerable - if (IsIEnum(type) || type.GetInterfaces().Any(IsIEnum)) - return typeof(object); - - return null; - - bool IsIEnum(Type t) => t == typeof(System.Collections.IEnumerable); - bool ImplIEnumT(Type t) => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IEnumerable<>); - } /// Gets a named field value from an object. /// The expected type of the field to be returned. @@ -83,16 +23,6 @@ namespace Vanara.Extensions return (T)obj.GetType().InvokeMember(fieldName, BindingFlags.GetField | bindingFlags, null, obj, null, null); } - /// Gets a named field value from an object. - /// The expected type of the field to be returned. - /// Name of the field. - /// The field value. - public static T GetStaticFieldValue(string fieldName) - { - if (string.IsNullOrEmpty(fieldName)) throw new ArgumentNullException(nameof(fieldName)); - return (T)typeof(T).InvokeMember(fieldName, BindingFlags.GetField | staticBindingFlags, null, null, null); - } - /// Gets a named field value from an object. /// The expected type of the field to be returned. /// The object from which to retrieve the field. @@ -148,31 +78,6 @@ namespace Vanara.Extensions return gmi.Invoke(obj, args); } - /// Invokes a named method on a created instance of a type with parameters. - /// The expected type of the method's return value. - /// The type to be instantiated and then used to invoke the method. This method assumes the type has a default public constructor. - /// Name of the method. - /// The arguments to provide to the method invocation. - /// The value returned from the method. - public static T InvokeMethod(this Type type, string methodName, params object[] args) - { - var o = Activator.CreateInstance(type); - return InvokeMethod(o, methodName, args); - } - - /// Invokes a named method on a created instance of a type with parameters. - /// The expected type of the method's return value. - /// The type to be instantiated and then used to invoke the method. - /// The arguments to supply to the constructor. - /// Name of the method. - /// The arguments to provide to the method invocation. - /// The value returned from the method. - public static T InvokeMethod(this Type type, object[] instArgs, string methodName, params object[] args) - { - var o = Activator.CreateInstance(type, instArgs); - return InvokeMethod(o, methodName, args); - } - /// Invokes a named method on an object with parameters and no return value. /// The object on which to invoke the method. /// Name of the method. @@ -226,6 +131,142 @@ namespace Vanara.Extensions return (T)mi.Invoke(obj, args); } + /// Sets a named field on an object. + /// The type of the field to be set. + /// The object on which to set the field. + /// Name of the field. + /// The field value to set on the object. + public static void SetFieldValue(this object obj, string fieldName, T value) + { + try { obj?.GetType().InvokeMember(fieldName, BindingFlags.SetField | bindingFlags, null, obj, new object[] { value }, null); } + catch { } + } + + /// Sets a named property on an object. + /// The type of the property to be set. + /// The object on which to set the property. + /// Name of the property. + /// The property value to set on the object. + public static void SetPropertyValue(this object obj, string propName, T value) + { + try { obj?.GetType().InvokeMember(propName, BindingFlags.SetProperty | bindingFlags, null, obj, new object[] { value }, null); } + catch { } + } + } +} + +namespace Vanara.Extensions +{ + using Vanara.Extensions.Reflection; + + /// Extensions related to System.Reflection + public static class ReflectionExtensions + { + private const BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.IgnoreCase; + private const BindingFlags staticBindingFlags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.IgnoreCase; + + /// For a structure, gets either the result of the static Create method or the default. + /// The structure's type. + /// The result of the static Create method or the default. + public static T CreateOrDefault() where T : struct + { + var mi = typeof(T).GetMethod("Create", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static, null, Type.EmptyTypes, null); + if (mi != null) + return (T)mi.Invoke(null, null); + var fi = typeof(T).GetField("Default", BindingFlags.Public | BindingFlags.Static); + if (fi != null) + return (T)fi.GetValue(null); + var pi = typeof(T).GetProperty("Default", BindingFlags.Public | BindingFlags.Static | BindingFlags.GetProperty, null, typeof(T), Type.EmptyTypes, null); + if (pi != null) + return (T)pi.GetValue(null); + return default; + } + + /// Gets all loaded types in the . + /// The application domain. + /// All loaded types. + public static IEnumerable GetAllTypes(this AppDomain appDomain) => appDomain.GetAssemblies().SelectMany(a => a.GetTypes()); + + /// Returns an array of custom attributes applied to this member and identified by . + /// The type of attribute to search for. Only attributes that are assignable to this type are returned. + /// An object derived from the MemberInfo class that describes a constructor, event, field, method, or property member of a class. + /// true to search this member's inheritance chain to find the attributes; otherwise, false. This parameter is ignored for properties and events. + /// An optional predicate to refine the results. + /// + public static IEnumerable GetCustomAttributes(this MemberInfo element, bool inherit = false, Func predicate = null) where TAttr : Attribute => + element.GetCustomAttributes(typeof(TAttr), inherit).Cast().Where(predicate ?? (a => true)); + + /// Returns an array of custom attributes applied to this member and identified by . + /// The type of attribute to search for. Only attributes that are assignable to this type are returned. + /// The type of the to examine. + /// true to search this member's inheritance chain to find the attributes; otherwise, false. This parameter is ignored for properties and events. + /// An optional predicate to refine the results. + /// + public static IEnumerable GetCustomAttributes(this Type type, bool inherit = false, Func predicate = null) where TAttr : Attribute => + type.GetCustomAttributes(typeof(TAttr), inherit).Cast().Where(predicate ?? (a => true)); + + /// Finds the type of the element of a type. Returns null if this type does not enumerate. + /// The type to check. + /// The element type, if found; otherwise, . + public static Type FindElementType(this Type type) + { + if (type.IsArray) + return type.GetElementType(); + + // type is IEnumerable; + if (ImplIEnumT(type)) + return type.GetGenericArguments().First(); + + // type implements/extends IEnumerable; + var enumType = type.GetInterfaces().Where(ImplIEnumT).Select(t => t.GetGenericArguments().First()).FirstOrDefault(); + if (enumType != null) + return enumType; + + // type is IEnumerable + if (IsIEnum(type) || type.GetInterfaces().Any(IsIEnum)) + return typeof(object); + + return null; + + bool IsIEnum(Type t) => t == typeof(System.Collections.IEnumerable); + bool ImplIEnumT(Type t) => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IEnumerable<>); + } + + /// Gets a named field value from an object. + /// The expected type of the field to be returned. + /// Name of the field. + /// The field value. + public static T GetStaticFieldValue(string fieldName) + { + if (string.IsNullOrEmpty(fieldName)) throw new ArgumentNullException(nameof(fieldName)); + return (T)typeof(T).InvokeMember(fieldName, BindingFlags.GetField | staticBindingFlags, null, null, null); + } + + /// Invokes a named method on a created instance of a type with parameters. + /// The expected type of the method's return value. + /// The type to be instantiated and then used to invoke the method. This method assumes the type has a default public constructor. + /// Name of the method. + /// The arguments to provide to the method invocation. + /// The value returned from the method. + public static T InvokeMethod(this Type type, string methodName, params object[] args) + { + var o = Activator.CreateInstance(type); + return o.InvokeMethod(methodName, args); + } + + /// Invokes a named method on a created instance of a type with parameters. + /// The expected type of the method's return value. + /// The type to be instantiated and then used to invoke the method. + /// The arguments to supply to the constructor. + /// Name of the method. + /// The arguments to provide to the method invocation. + /// The value returned from the method. + public static T InvokeMethod(this Type type, object[] instArgs, string methodName, params object[] args) + { + var o = Activator.CreateInstance(type, instArgs); + return o.InvokeMethod(methodName, args); + } + /// Invokes a named static method of a type with parameters. /// The expected type of the method's return value. /// The type containing the static method. @@ -298,28 +339,6 @@ namespace Vanara.Extensions return ret; } - /// Sets a named field on an object. - /// The type of the field to be set. - /// The object on which to set the field. - /// Name of the field. - /// The field value to set on the object. - public static void SetFieldValue(this object obj, string fieldName, T value) - { - try { obj?.GetType().InvokeMember(fieldName, BindingFlags.SetField | bindingFlags, null, obj, new object[] { value }, null); } - catch { } - } - - /// Sets a named property on an object. - /// The type of the property to be set. - /// The object on which to set the property. - /// Name of the property. - /// The property value to set on the object. - public static void SetPropertyValue(this object obj, string propName, T value) - { - try { obj?.GetType().InvokeMember(propName, BindingFlags.SetProperty | bindingFlags, null, obj, new object[] { value }, null); } - catch { } - } - /// Tries the retrieve a reference from an assembly. /// Name of the type. /// The assembly from which to load the type. diff --git a/PInvoke/ComCtl32/CommCtrl.Header.cs b/PInvoke/ComCtl32/CommCtrl.Header.cs index d457fc41..e379211b 100644 --- a/PInvoke/ComCtl32/CommCtrl.Header.cs +++ b/PInvoke/ComCtl32/CommCtrl.Header.cs @@ -705,12 +705,12 @@ namespace Vanara.PInvoke break; case DateTime dt: - pvFilter = new SYSTEMTIME(dt).StructureToPtr(Marshal.AllocCoTaskMem, out var _); + pvFilter = new SYSTEMTIME(dt).MarshalToPtr(Marshal.AllocCoTaskMem, out var _); type = HeaderItemFilterType.HDFT_ISDATE; break; case string str: - pvFilter = new HDTEXTFILTER(str).StructureToPtr(Marshal.AllocCoTaskMem, out var _); + pvFilter = new HDTEXTFILTER(str).MarshalToPtr(Marshal.AllocCoTaskMem, out var _); type = HeaderItemFilterType.HDFT_ISSTRING; break; @@ -720,7 +720,7 @@ namespace Vanara.PInvoke break; case SYSTEMTIME st: - pvFilter = st.StructureToPtr(Marshal.AllocCoTaskMem, out var _); + pvFilter = st.MarshalToPtr(Marshal.AllocCoTaskMem, out var _); type = HeaderItemFilterType.HDFT_ISDATE; break; @@ -929,7 +929,7 @@ namespace Vanara.PInvoke /// The coordinates of the header. public HDLAYOUT(RECT rc) { - prc = rc.StructureToPtr(Marshal.AllocHGlobal, out var _); + prc = rc.MarshalToPtr(Marshal.AllocHGlobal, out var _); pwpos = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(WINDOWPOS))); } @@ -941,7 +941,7 @@ namespace Vanara.PInvoke set { Marshal.FreeHGlobal(prc); - prc = value.StructureToPtr(Marshal.AllocHGlobal, out var _); + prc = value.MarshalToPtr(Marshal.AllocHGlobal, out var _); } } diff --git a/PInvoke/ComCtl32/CommCtrl.ListView.cs b/PInvoke/ComCtl32/CommCtrl.ListView.cs index 3bf3493a..e2205a09 100644 --- a/PInvoke/ComCtl32/CommCtrl.ListView.cs +++ b/PInvoke/ComCtl32/CommCtrl.ListView.cs @@ -2159,14 +2159,12 @@ namespace Vanara.PInvoke } if (cColumns > 0) { - puColumns = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)) * (int)cColumns); - cols.MarshalToPtr(puColumns); + puColumns = cols.MarshalToPtr(Marshal.AllocHGlobal, out _); } EnumExtensions.SetFlags(ref mask, ListViewItemMask.LVIF_COLUMNS); if (hasFmts) { - piColFmt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)) * (int)cColumns); - fmts.MarshalToPtr(piColFmt); + piColFmt = fmts.MarshalToPtr(Marshal.AllocHGlobal, out _); EnumExtensions.SetFlags(ref mask, ListViewItemMask.LVIF_COLFMT); } } diff --git a/PInvoke/Kernel32/ErrHandlingApi.cs b/PInvoke/Kernel32/ErrHandlingApi.cs index 04c8a06d..b9200284 100644 --- a/PInvoke/Kernel32/ErrHandlingApi.cs +++ b/PInvoke/Kernel32/ErrHandlingApi.cs @@ -2,6 +2,7 @@ using System.Runtime.InteropServices; using System.Text; using Vanara.Extensions; +using Vanara.Extensions.Reflection; using Vanara.InteropServices; namespace Vanara.PInvoke @@ -690,7 +691,7 @@ namespace Vanara.PInvoke flags |= FormatMessageFlags.FORMAT_MESSAGE_IGNORE_INSERTS; Win32Error lastError; var buf = new StringBuilder(1024); - using (var pargs = new SafeHGlobalHandle(args.MarshalObjectsToPtr(Marshal.AllocHGlobal, out var sz, true), sz, true)) + using (var pargs = new SafeHGlobalHandle(InteropExtensions.MarshalObjectsToPtr(args, Marshal.AllocHGlobal, out var sz, true), sz, true)) do { if (0 != FormatMessage(flags, hLib, id, langId, buf, (uint)buf.Capacity, (IntPtr)pargs)) @@ -735,7 +736,7 @@ namespace Vanara.PInvoke flags |= FormatMessageFlags.FORMAT_MESSAGE_IGNORE_INSERTS; Win32Error lastError; var buf = new StringBuilder(1024); - using (var pargs = new SafeHGlobalHandle(args.MarshalObjectsToPtr(Marshal.AllocHGlobal, out var sz, true), sz, true)) + using (var pargs = new SafeHGlobalHandle(InteropExtensions.MarshalObjectsToPtr(args, Marshal.AllocHGlobal, out var sz, true), sz, true)) do { if (0 != FormatMessage(flags, formatString, 0, 0, buf, (uint)buf.Capacity, (IntPtr)pargs)) diff --git a/PInvoke/NTDll/NTDll.cs b/PInvoke/NTDll/NTDll.cs index be4d146b..24d7116a 100644 --- a/PInvoke/NTDll/NTDll.cs +++ b/PInvoke/NTDll/NTDll.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Runtime.InteropServices; using System.Text; using Vanara.Extensions; +using Vanara.Extensions.Reflection; using Vanara.InteropServices; using static Vanara.PInvoke.Kernel32; @@ -1325,7 +1326,7 @@ namespace Vanara.PInvoke // EnlistmentKey ); [DllImport(Lib.NtDll, SetLastError = false, ExactSpelling = true)] [PInvokeData("wdm.h", MSDNShortId = "5ffd8262-10b3-4c40-bd3e-050271338508")] - public static extern NTStatus NtCreateEnlistment(out SafeEnlistmentHandle EnlistmentHandle, ACCESS_MASK DesiredAccess, SafeResourceManagerHandle ResourceManagerHandle, + public static extern NTStatus NtCreateEnlistment(out SafeEnlistmentHandle EnlistmentHandle, ACCESS_MASK DesiredAccess, SafeResourceManagerHandle ResourceManagerHandle, SafeTransactionHandle TransactionHandle, ref OBJECT_ATTRIBUTES ObjectAttributes, uint CreateOptions, NOTIFICATION_MASK NotificationMask, [Optional] IntPtr EnlistmentKey); /// @@ -1989,7 +1990,7 @@ namespace Vanara.PInvoke // RmGuid, POBJECT_ATTRIBUTES ObjectAttributes, ULONG CreateOptions, PUNICODE_STRING Description ); [DllImport(Lib.NtDll, SetLastError = false, ExactSpelling = true)] [PInvokeData("wdm.h", MSDNShortId = "4812eeb4-134f-4ecb-870b-dbab04c1137b")] - public static extern NTStatus NtCreateResourceManager(out SafeResourceManagerHandle ResourceManagerHandle, ACCESS_MASK DesiredAccess, SafeTransactionManagerHandle TmHandle, in Guid RmGuid, + public static extern NTStatus NtCreateResourceManager(out SafeResourceManagerHandle ResourceManagerHandle, ACCESS_MASK DesiredAccess, SafeTransactionManagerHandle TmHandle, in Guid RmGuid, ref OBJECT_ATTRIBUTES ObjectAttributes, uint CreateOptions, in UNICODE_STRING Description); /// Provides a to an enlistment that releases its handle at disposal using NTClose. @@ -2180,7 +2181,7 @@ namespace Vanara.PInvoke // MaximumSize, ULONG SectionPageProtection, ULONG AllocationAttributes, HANDLE FileHandle ); [DllImport(Lib.NtDll, SetLastError = false, CharSet = CharSet.Auto)] [PInvokeData("ntifs.h", MSDNShortId = "805d7eff-19be-47a1-acc9-1b97e5493031")] - public static extern NTStatus NtCreateSection(out SafeSectionHandle SectionHandle, ACCESS_MASK DesiredAccess, ref OBJECT_ATTRIBUTES ObjectAttributes, ref long MaximumSize, + public static extern NTStatus NtCreateSection(out SafeSectionHandle SectionHandle, ACCESS_MASK DesiredAccess, ref OBJECT_ATTRIBUTES ObjectAttributes, ref long MaximumSize, MEM_PROTECTION SectionPageProtection, SEC_ALLOC AllocationAttributes, [Optional] HFILE FileHandle); /// The ZwCreateTransaction routine creates a transaction object.A pointer to a caller-allocated variable that receives a handle to the new transaction object, if the call to ZwCreateTransaction succeeds.An ACCESS_MASK value that specifies the caller's requested access to the transaction object. In addition to the access rights that are defined for all kinds of objects (see ACCESS_MASK), the caller can specify any of the following flags for transaction objects.Access maskAllows the caller toTRANSACTION_COMMIT Commit the transaction (see ZwCommitTransaction). TRANSACTION_ENLIST Create an enlistment for the transaction (see ZwCreateEnlistment). TRANSACTION_PROPAGATEDo not use.TRANSACTION_QUERY_INFORMATION Obtain information about the transaction (see ZwQueryInformationTransaction). TRANSACTION_ROLLBACK Roll back the transaction (see ZwRollbackTransaction). TRANSACTION_SET_INFORMATION Set information for the transaction (see ZwSetInformationTransaction). Alternatively, you can specify one or more of the following ACCESS_MASK bitmaps. These bitmaps combine the flags from the previous table with the STANDARD_RIGHTS_XXX flags that are described on the ACCESS_MASK reference page. You can also combine these bitmaps with additional flags from the preceding table. The following table shows how the bitmaps correspond to specific access rights.Rights bitmapSet of specific access rightsTRANSACTION_GENERIC_READSTANDARD_RIGHTS_READ, TRANSACTION_QUERY_INFORMATION, and SYNCHRONIZETRANSACTION_GENERIC_WRITESTANDARD_RIGHTS_WRITE, TRANSACTION_SET_INFORMATION, TRANSACTION_COMMIT, TRANSACTION_ENLIST, TRANSACTION_ROLLBACK, TRANSACTION_PROPAGATE, TRANSACTION_SAVEPOINT, and SYNCHRONIZETRANSACTION_GENERIC_EXECUTESTANDARD_RIGHTS_EXECUTE, TRANSACTION_COMMIT, TRANSACTION_ROLLBACK, and SYNCHRONIZETRANSACTION_ALL_ACCESSSTANDARD_RIGHTS_REQUIRED, TRANSACTION_GENERIC_READ, TRANSACTION_GENERIC_WRITE, and TRANSACTION_GENERIC_EXECUTETRANSACTION_RESOURCE_MANAGER_RIGHTSSTANDARD_RIGHTS_WRITE, TRANSACTION_GENERIC_READ, TRANSACTION_SET_INFORMATION, TRANSACTION_ENLIST, TRANSACTION_ROLLBACK, TRANSACTION_PROPAGATE, and SYNCHRONIZETypically, a resource manager specifies TRANSACTION_RESOURCE_MANAGER_RIGHTS.The DesiredAccess value cannot be zero.A pointer to an OBJECT_ATTRIBUTES structure that specifies the object name and other attributes. Use the InitializeObjectAttributes routine to initialize this structure. If the caller is not running in a system thread context, it must set the OBJ_KERNEL_HANDLE attribute when it calls InitializeObjectAttributes. This parameter is optional and can be NULL.A pointer to a GUID that KTM uses as the new transaction object's unit of work (UOW) identifier. This parameter is optional and can be NULL. If this parameter is NULL, KTM generates a GUID and assigns it to the transaction object. For more information, see the following Remarks section.A handle to a transaction manager object that was obtained by a previous call to ZwCreateTransactionManager or ZwOpenTransactionManager. KTM assigns the new transaction object to the specified transaction manager object. If this parameter is NULL, KTM assigns the new transaction object to a transaction manager later, when a resource manager creates an enlistment for the transaction.Optional object creation flags. The following table contains the available flags, which are defined in Ktmtypes.h.Option flagMeaningTRANSACTION_DO_NOT_PROMOTEReserved for future use.Reserved for future use. Callers must set this parameter to zero.Reserved for future use. Callers should set this parameter to zero.A pointer to a time-out value. If the transaction has not been committed by the time specified by this parameter, KTM rolls back the transaction. The time-out value is expressed in system time units (100-nanosecond intervals), and can specify either an absolute time or a relative time. If the value pointed to by Timeout is negative, the expiration time is relative to the current system time. Otherwise, the expiration time is absolute. This pointer is optional and can be NULL if you do not want the transaction to have a time-out value. If Timeout = NULL or *Timeout = 0, the transaction never times out. (You can also use ZwSetInformationTransaction to set a time-out value.)A pointer to a caller-supplied UNICODE_STRING structure that contains a NULL-terminated string. The string provides a description of the transaction. KTM stores a copy of the string and includes the string in messages that it writes to the log stream. The maximum string length is MAX_TRANSACTION_DESCRIPTION_LENGTH. This parameter is optional and can be NULL.ZwCreateTransaction returns STATUS_SUCCESS if the operation succeeds. Otherwise, this routine might return one of the following values:Return codeDescription STATUS_INVALID_PARAMETER The CreateOptions parameter contains an invalid flag, the DesiredAccess parameter is zero, or the Description parameter's string is too long. STATUS_INSUFFICIENT_RESOURCES KTM could not allocate system resources (typically memory). STATUS_INVALID_ACL A security descriptor contains an invalid access control list (ACL). STATUS_INVALID_SID A security descriptor contains an invalid security identifier (SID). STATUS_OBJECT_NAME_EXISTS The object name that the ObjectAttributes parameter specifies already exists. STATUS_OBJECT_NAME_INVALID The object name that the ObjectAttributes parameter specifies is invalid. STATUS_ACCESS_DENIED The value of the DesiredAccess parameter is invalid. The routine might return other NTSTATUS values.The caller can use the Uow parameter to specify a UOW identifier for the transaction object. If the caller does not specify a UOW identifier, KTM generates a GUID and assigns it to the transaction object. The caller can later obtain this GUID by calling ZwQueryInformationTransaction.Typically, you should let KTM generate a GUID for the transaction object, unless your component communicates with another TPS component that has already generated a UOW identifier for the transaction.To close the transaction handle, the component that called ZwCreateTransaction must call ZwClose. If the last transaction handle closes before any component calls ZwCommitTransaction for the transaction, KTM rolls back the transaction.For more information about how transaction clients should use ZwCreateTransaction, see Creating a Transactional Client.For calls from kernel-mode drivers, the NtXxx and ZwXxx versions of a Windows Native System Services routine can behave differently in the way that they handle and interpret input parameters. For more information about the relationship between the NtXxx and ZwXxx versions of a routine, see Using Nt and Zw Versions of the Native System Services Routines. @@ -2196,7 +2197,7 @@ namespace Vanara.PInvoke // __kernel_entry NTSYSCALLAPI NTSTATUS NtCreateTransactionManager( PHANDLE TmHandle, ACCESS_MASK DesiredAccess, POBJECT_ATTRIBUTES ObjectAttributes, PUNICODE_STRING LogFileName, ULONG CreateOptions, ULONG CommitStrength ); [DllImport(Lib.NtDll, SetLastError = false, CharSet = CharSet.Auto)] [PInvokeData("wdm.h", MSDNShortId = "9c9f0a8b-7add-4ab1-835d-39f508ce32a9")] - public static extern NTStatus NtCreateTransactionManager(out SafeTransactionManagerHandle TmHandle, ACCESS_MASK DesiredAccess, ref OBJECT_ATTRIBUTES ObjectAttributes, + public static extern NTStatus NtCreateTransactionManager(out SafeTransactionManagerHandle TmHandle, ACCESS_MASK DesiredAccess, ref OBJECT_ATTRIBUTES ObjectAttributes, in UNICODE_STRING LogFileName, [Optional] uint CreateOptions, [Optional] uint CommitStrength); /// diff --git a/Security/ActiveDirectoryExtension.cs b/Security/ActiveDirectoryExtension.cs index 667119b4..74b74359 100644 --- a/Security/ActiveDirectoryExtension.cs +++ b/Security/ActiveDirectoryExtension.cs @@ -2,6 +2,7 @@ using System; using System.Linq; using System.DirectoryServices.ActiveDirectory; +using Vanara.Extensions.Reflection; using static Vanara.PInvoke.NTDSApi; namespace Vanara.Extensions diff --git a/UnitTests/Core/Extensions/ReflectionExtensionsTests.cs b/UnitTests/Core/Extensions/ReflectionExtensionsTests.cs index 0d624180..0b79697a 100644 --- a/UnitTests/Core/Extensions/ReflectionExtensionsTests.cs +++ b/UnitTests/Core/Extensions/ReflectionExtensionsTests.cs @@ -1,4 +1,5 @@ using Vanara.Extensions; +using Vanara.Extensions.Reflection; using NUnit.Framework; using System; using System.Drawing; @@ -109,7 +110,7 @@ namespace Vanara.Extensions.Tests Assert.That(() => dt.InvokeMethod("ToBin"), Throws.Exception); Assert.That(() => dt.InvokeMethod("ToBinary", 1), Throws.Exception); Assert.That(() => dt.InvokeMethod("ToBinary", 1), Throws.Exception); - Assert.That(() => dt.InvokeMethod("Subtract", new[] {typeof(long)}, new object[] {1}), Throws.ArgumentException); + Assert.That(() => dt.InvokeMethod("Subtract", new[] { typeof(long) }, new object[] { 1 }), Throws.ArgumentException); Assert.That(() => dt.InvokeMethod("Subtract", new[] { typeof(DateTime) }, new object[] { 1 }), Throws.ArgumentException); Assert.That(() => dt.InvokeMethod("Subtract", new[] { typeof(DateTime) }, new object[] { DateTime.Now }), Throws.ArgumentException); } diff --git a/WIndows.Forms/Components/MRUManager.cs b/WIndows.Forms/Components/MRUManager.cs index 4d735e41..7ed66335 100644 --- a/WIndows.Forms/Components/MRUManager.cs +++ b/WIndows.Forms/Components/MRUManager.cs @@ -4,6 +4,7 @@ using System.Collections.Specialized; using System.ComponentModel; using System.Linq; using Vanara.Extensions; +using Vanara.Extensions.Reflection; namespace Vanara.Configuration { @@ -495,7 +496,7 @@ namespace Vanara.Windows.Forms /// Action to run when The on recent file click. /// Optional. The on clear recent files click. /// Optional. The storage handler. - public MenuStripMRUManager(string extensions, ToolStripMenuItem parentMenuItem, Action onRecentFileClick, + public MenuStripMRUManager(string extensions, ToolStripMenuItem parentMenuItem, Action onRecentFileClick, Action onClearRecentFilesClick = null, IFileListStorage storageHandler = null) : base(storageHandler ?? new AppSettingsFileListStorage(), new MenuStripMenuBuilder()) { @@ -571,7 +572,7 @@ namespace Vanara.Windows.Forms if (!string.IsNullOrEmpty(clearListMenuItemText)) { RecentFileMenuItem.DropDownItems.Add("-"); - RecentFileMenuItem.DropDownItems.Add(clearListMenuItemText, null, (o, e) => clearListMenuItemClick?.Invoke() ); + RecentFileMenuItem.DropDownItems.Add(clearListMenuItemText, null, (o, e) => clearListMenuItemClick?.Invoke()); } RecentFileMenuItem.Enabled = true; } diff --git a/WIndows.Forms/Dialogs/AccessControlEditor/SecuredObject.cs b/WIndows.Forms/Dialogs/AccessControlEditor/SecuredObject.cs index 6b12f4b0..7cf98774 100644 --- a/WIndows.Forms/Dialogs/AccessControlEditor/SecuredObject.cs +++ b/WIndows.Forms/Dialogs/AccessControlEditor/SecuredObject.cs @@ -2,6 +2,7 @@ using System.Reflection; using System.Security.AccessControl; using Vanara.Extensions; +using Vanara.Extensions.Reflection; namespace Vanara.Security.AccessControl { diff --git a/WIndows.Forms/Extensions/ControlExtensionGDI.cs b/WIndows.Forms/Extensions/ControlExtensionGDI.cs index 32b2eebc..e634a1f8 100644 --- a/WIndows.Forms/Extensions/ControlExtensionGDI.cs +++ b/WIndows.Forms/Extensions/ControlExtensionGDI.cs @@ -1,5 +1,6 @@ using System.Drawing; using System.Windows.Forms; +using Vanara.Extensions.Reflection; namespace Vanara.Extensions { diff --git a/Windows.Shell/BindContext.cs b/Windows.Shell/BindContext.cs index fa435097..58e27de6 100644 --- a/Windows.Shell/BindContext.cs +++ b/Windows.Shell/BindContext.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Runtime.InteropServices; using System.Runtime.InteropServices.ComTypes; using Vanara.Extensions; +using Vanara.Extensions.Reflection; using Vanara.PInvoke; using static Vanara.PInvoke.Ole32; diff --git a/Windows.Shell/ShellExtensions/ComClassFactory.cs b/Windows.Shell/ShellExtensions/ComClassFactory.cs index 8bb682fd..997dfa88 100644 --- a/Windows.Shell/ShellExtensions/ComClassFactory.cs +++ b/Windows.Shell/ShellExtensions/ComClassFactory.cs @@ -1,6 +1,6 @@ using System; using System.Runtime.InteropServices; -using Vanara.Extensions; +using Vanara.Extensions.Reflection; using Vanara.PInvoke; using static Vanara.PInvoke.Ole32;