Exemple #1
0
        public void CreateDelegateFromStaticMethodByName()
        {
            Action action   = Delegates.CreateDelegate <Action>(GetType(), "StaticActionMethod");
            Action expected = StaticActionMethod;

            Assert.AreEqual(expected, action);
        }
Exemple #2
0
        public void CreateDelegateFromStaticMethodByNameCaseInsensitively()
        {
            Action action   = Delegates.CreateDelegate <Action>(GetType(), "staticactionmethod", true);
            Action expected = StaticActionMethod;

            Assert.AreEqual(expected, action);
        }
Exemple #3
0
        public void CreateDelegateFromInstanceMethodByName()
        {
            Action action   = Delegates.CreateDelegate <Action>(this, "InstanceActionMethod");
            Action expected = InstanceActionMethod;

            Assert.AreEqual(expected, action);
        }
Exemple #4
0
        public void CreateDelegateFromInstanceMethodByNameCaseInsensitively()
        {
            Action action   = Delegates.CreateDelegate <Action>(this, "INSTANCEACTIONMETHOD", true);
            Action expected = InstanceActionMethod;

            Assert.AreEqual(expected, action);
        }
Exemple #5
0
        /// <summary>
        /// CreateFile wrapper that attempts to use CreateFile2 if running as Windows Store app.
        /// </summary>
        public static SafeFileHandle CreateFile(
            StringSpan path,
            CreationDisposition creationDisposition,
            DesiredAccess desiredAccess       = DesiredAccess.GenericReadWrite,
            ShareModes shareMode              = ShareModes.ReadWrite,
            FileAttributes fileAttributes     = FileAttributes.None,
            FileFlags fileFlags               = FileFlags.None,
            SecurityQosFlags securityQosFlags = SecurityQosFlags.None)
        {
            // Prefer CreateFile2, falling back to CreateFileEx if we can
            if (s_createFileDelegate == null)
            {
                s_createFileDelegate = CreateFile2;
                try
                {
                    return(s_createFileDelegate(path, desiredAccess, shareMode, creationDisposition, fileAttributes, fileFlags, securityQosFlags));
                }
                catch (EntryPointNotFoundException)
                {
                    s_createFileDelegate = Delegates.CreateDelegate <CreateFileDelegate>(
                        "WInterop.Storage.Desktop.NativeMethods, " + Delegates.DesktopLibrary,
                        "CreateFileW");
                }
            }

            return(s_createFileDelegate(path, desiredAccess, shareMode, creationDisposition, fileAttributes, fileFlags, securityQosFlags));
        }
Exemple #6
0
        /// <summary>
        /// CopyFile wrapper that attempts to use CopyFile2 if running as Windows Store app.
        /// </summary>
        public static void CopyFile(string source, string destination, bool overwrite = false)
        {
            // Prefer CreateFile2, falling back to CreateFileEx if we can
            if (s_copyFileDelegate == null)
            {
                s_copyFileDelegate = CopyFile2;
                try
                {
                    s_copyFileDelegate(source, destination, overwrite);
                    return;
                }
                catch (Exception exception)
                {
                    // Any error other than EntryPointNotFound we've found CreateFile2, rethrow
                    if (!ErrorHelper.IsEntryPointNotFoundException(exception))
                    {
                        throw;
                    }

                    s_copyFileDelegate = Delegates.CreateDelegate <CopyFileDelegate>(
                        "WInterop.FileManagement.Desktop.NativeMethods, " + Delegates.DesktopLibrary,
                        "CopyFileEx");
                }
            }

            s_copyFileDelegate(source, destination, overwrite);
        }
Exemple #7
0
        /// <summary>
        /// CreateFile wrapper that attempts to use CreateFile2 if running as Windows Store app.
        /// </summary>
        public static SafeFileHandle CreateFile(
            string path,
            DesiredAccess desiredAccess,
            ShareMode shareMode,
            CreationDisposition creationDisposition,
            FileAttributes fileAttributes     = FileAttributes.NONE,
            FileFlags fileFlags               = FileFlags.NONE,
            SecurityQosFlags securityQosFlags = SecurityQosFlags.NONE)
        {
            // Prefer CreateFile2, falling back to CreateFileEx if we can
            if (s_createFileDelegate == null)
            {
                s_createFileDelegate = CreateFile2;
                try
                {
                    return(s_createFileDelegate(path, desiredAccess, shareMode, creationDisposition, fileAttributes, fileFlags, securityQosFlags));
                }
                catch (Exception exception)
                {
                    // Any error other than EntryPointNotFound we've found CreateFile2, rethrow
                    if (!ErrorHelper.IsEntryPointNotFoundException(exception))
                    {
                        throw;
                    }

                    s_createFileDelegate = Delegates.CreateDelegate <CreateFileDelegate>(
                        "WInterop.FileManagement.Desktop.NativeMethods, " + Delegates.DesktopLibrary,
                        "CreateFileW");
                }
            }

            return(s_createFileDelegate(path, desiredAccess, shareMode, creationDisposition, fileAttributes, fileFlags, securityQosFlags));
        }
        public void SimpleGetDelegate()
        {
            var beginsWithDirectorySeparator = Delegates.CreateDelegate <SpanToBool>(@"WInterop.Support.Paths, " + Delegates.DesktopLibrary, "BeginsWithDirectorySeparator");

            beginsWithDirectorySeparator.Should().NotBeNull();
            beginsWithDirectorySeparator(@"a").Should().BeFalse();
            beginsWithDirectorySeparator(@"\a").Should().BeTrue();
        }
Exemple #9
0
        public void SimpleGetDelegate()
        {
            var endsInDirectorySeparator = Delegates.CreateDelegate <StringToBool>(@"WInterop.Support.Paths, " + Delegates.DesktopLibrary, "EndsInDirectorySeparator");

            endsInDirectorySeparator.Should().NotBeNull();
            endsInDirectorySeparator(@"a").Should().BeFalse();
            endsInDirectorySeparator(@"a\").Should().BeTrue();
        }
Exemple #10
0
        public void CreateDelegateFromInstanceMethod()
        {
            MethodInfo method   = typeof(DelegatesTest).GetMethod("InstanceActionMethod");
            Action     action   = Delegates.CreateDelegate <Action>(this, method);
            Action     expected = InstanceActionMethod;

            Assert.AreEqual(expected, action);
        }
Exemple #11
0
        public void CreateDelegateFromStaticMethod()
        {
            MethodInfo method   = typeof(DelegatesTest).GetMethod("StaticActionMethod");
            Action     action   = Delegates.CreateDelegate <Action>(method);
            Action     expected = StaticActionMethod;

            Assert.AreEqual(expected, action);
        }
Exemple #12
0
        /// <summary>
        /// CopyFile wrapper that attempts to use CopyFile2 if running as Windows Store app.
        /// </summary>
        public static void CopyFile(string source, string destination, bool overwrite = false)
        {
            // Prefer CreateFile2, falling back to CreateFileEx if we can
            if (s_copyFileDelegate == null)
            {
                s_copyFileDelegate = CopyFile2;
                try
                {
                    s_copyFileDelegate(source, destination, overwrite);
                    return;
                }
                catch (EntryPointNotFoundException)
                {
                    s_copyFileDelegate = Delegates.CreateDelegate <CopyFileDelegate>(
                        "WInterop.Storage.Desktop.NativeMethods, " + Delegates.DesktopLibrary,
                        "CopyFileEx");
                }
            }

            s_copyFileDelegate(source, destination, overwrite);
        }
Exemple #13
0
        public void CreateDelegateWithoutTargetSuppressingBindFailure()
        {
            MethodInfo method = typeof(DelegatesTest).GetMethod("InstanceActionMethod");

            Assert.IsNull(Delegates.CreateDelegate <Action>(method, false));
        }
Exemple #14
0
 public void CreateDelegateFromStaticMethodByNameCaseSensitively()
 {
     Assert.IsNull(Delegates.CreateDelegate <Action>(GetType(), "staticactionmethod", false, false));
 }
Exemple #15
0
 public void CreateDelegateFromInstanceMethodByNameCaseSensitivelySuppressingBindFailure()
 {
     Assert.IsNull(Delegates.CreateDelegate <Action>(this, "INSTANCEACTIONMETHOD", false, false));
 }