/// <summary>
        /// Checks if the method is public or protected.
        /// </summary>
        /// <param name="method"></param>
        /// <returns></returns>
        protected bool IsAccessible(MethodBase method)
        {
            // Accessibility supported by the full framework and CoreCLR
            if (method.IsPublic || method.IsFamily || method.IsFamilyOrAssembly)
            {
                return(true);
            }

#if !SILVERLIGHT
            // Accessibility not supported by the CoreCLR
            if (method.IsFamilyAndAssembly)
            {
                return(true);
            }
            if (InternalsHelper.IsInternalToDynamicProxy(method.DeclaringType.Assembly) && method.IsAssembly)
            {
                return(true);
            }
#endif

            // Explicitly implemented interface method on class
            if (method.IsPrivate && method.IsFinal)
            {
                Logger.Debug("Excluded explicitly implemented interface method {0} on type {1} because it cannot be intercepted.",
                             method.Name, method.DeclaringType.FullName);
            }

            return(false);
        }
        /// <summary>
        /// Performs some basic screening and invokes the <see cref="IProxyGenerationHook"/>
        /// to select methods.
        /// </summary>
        /// <param name="method"></param>
        /// <param name="onlyVirtuals"></param>
        /// <param name="hook"></param>
        /// <returns></returns>
        protected bool AcceptMethod(MethodInfo method, bool onlyVirtuals, IProxyGenerationHook hook)
        {
            // we can never intercept a sealed (final) method
            if (method.IsFinal)
            {
                Logger.Debug("Excluded sealed method {0} on {1} because it cannot be intercepted.", method.Name, method.DeclaringType.FullName);
                return(false);
            }

            bool isInternalsAndNotVisibleToDynamicProxy = InternalsHelper.IsInternal(method);

            if (isInternalsAndNotVisibleToDynamicProxy)
            {
                isInternalsAndNotVisibleToDynamicProxy = InternalsHelper.IsInternalToDynamicProxy(method.DeclaringType.Assembly) ==
                                                         false;
            }

            if (isInternalsAndNotVisibleToDynamicProxy)
            {
                return(false);
            }

            if (onlyVirtuals && !method.IsVirtual)
            {
#if SILVERLIGHT
                if (method.DeclaringType != typeof(object))
#else
                if (method.DeclaringType != typeof(object) && method.DeclaringType != typeof(MarshalByRefObject))
#endif
                {
                    Logger.Debug("Excluded non-virtual method {0} on {1} because it cannot be intercepted.", method.Name, method.DeclaringType.FullName);
                    hook.NonVirtualMemberNotification(type, method);
                }

                return(false);
            }

            //can only proxy methods that are public or protected (or internals that have already been checked above)
            if ((method.IsPublic || method.IsFamily || method.IsAssembly || method.IsFamilyOrAssembly) == false)
            {
                return(false);
            }

            if (method.DeclaringType == typeof(object))
            {
                return(false);
            }

#if !SILVERLIGHT
            if (method.DeclaringType == typeof(MarshalByRefObject))
            {
                return(false);
            }
#endif
            return(hook.ShouldInterceptMethod(type, method));
        }
예제 #3
0
        public void GetDefaultFontReturnsAFont()
        {
            // Given

            // When
            Font font = InternalsHelper.GetDefaultFont();

            // Then
            Assert.IsNotNull(font);
        }
예제 #4
0
        public void GetUserDataFolderReturnsString()
        {
            // Given

            // When
            string userDataFolder = InternalsHelper.GetUserDataFolder();

            // Then
            Assert.IsNotNullOrEmpty(userDataFolder);
        }
        private bool IsConstructorVisible(ConstructorInfo constructor)
        {
            return(constructor.IsPublic ||
                   constructor.IsFamily ||
                   constructor.IsFamilyOrAssembly
#if !Silverlight
                   || (constructor.IsAssembly && InternalsHelper.IsInternalToDynamicProxy(constructor.DeclaringType.Assembly)));
#else
                   ;
#endif
        }
예제 #6
0
        public void GetIdentifierTokenValueTextReturnsNullForInvalidNode()
        {
            // Given
            SyntaxTree syntaxTree = Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree.ParseText("int c = 0;",
                                                                                             InternalsHelper.GetCSharpParseOptionsForSourceCodeKind(SourceCodeKind.Script));
            SyntaxNode syntaxNode = syntaxTree.GetRoot();  // The root will never have an identifier token

            // When
            string tokenValueText = InternalsHelper.GetIdentifierTokenValueText(syntaxNode);

            // Then
            Assert.IsNull(tokenValueText);
        }
예제 #7
0
        public void GetIdentifierTokenValueTextReturnsTokenValueTextForValidNode()
        {
            // Given
            SyntaxTree syntaxTree = Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree.ParseText("int c = 0;",
                                                                                             InternalsHelper.GetCSharpParseOptionsForSourceCodeKind(SourceCodeKind.Script));
            SyntaxToken syntaxToken = syntaxTree.GetRoot()
                                      .DescendantNodesAndTokens()
                                      .Where(x => x.IsToken)
                                      .Select(x => x.AsToken())
                                      .First(x => Microsoft.CodeAnalysis.CSharp.CSharpExtensions.Kind(x) == Microsoft.CodeAnalysis.CSharp.SyntaxKind.IdentifierToken);
            SyntaxNode syntaxNode = syntaxToken.Parent;

            // When
            string tokenValueText = InternalsHelper.GetIdentifierTokenValueText(syntaxNode);

            // Then
            Assert.AreEqual("c", tokenValueText);
        }
예제 #8
0
        private MethodAttributes ObtainAttributes()
        {
            var methodInfo = Method;
            var attributes = MethodAttributes.Virtual;

            if (methodInfo.IsFinal || Method.DeclaringType.IsInterface)
            {
                attributes |= MethodAttributes.NewSlot;
            }

            if (methodInfo.IsPublic)
            {
                attributes |= MethodAttributes.Public;
            }

            if (methodInfo.IsHideBySig)
            {
                attributes |= MethodAttributes.HideBySig;
            }
            if (InternalsHelper.IsInternal(methodInfo) &&
                InternalsHelper.IsInternalToDynamicProxy(methodInfo.DeclaringType.Assembly))
            {
                attributes |= MethodAttributes.Assembly;
            }
            if (methodInfo.IsFamilyAndAssembly)
            {
                attributes |= MethodAttributes.FamANDAssem;
            }
            else if (methodInfo.IsFamilyOrAssembly)
            {
                attributes |= MethodAttributes.FamORAssem;
            }
            else if (methodInfo.IsFamily)
            {
                attributes |= MethodAttributes.Family;
            }

            if (Standalone == false)
            {
                attributes |= MethodAttributes.SpecialName;
            }
            return(attributes);
        }
예제 #9
0
        /// <summary>
        /// Validates that the target type to proxy is visible and not generic.
        /// </summary>
        /// <param name="target">
        /// The interface type to proxy.
        /// </param>
        private static void AssertValidType(Type target)
        {
            // This is copied from the DefaultProxyBuilder because we need to
            // validate types but the validation logic is not accessible.

            bool isTargetNested      = target.IsNested;
            bool isNestedAndInternal = isTargetNested && (target.IsNestedAssembly || target.IsNestedFamORAssem);
            bool isInternalNotNested = target.IsVisible == false && isTargetNested == false;

            bool internalAndVisibleToDynProxy = (isInternalNotNested || isNestedAndInternal) &&
                                                InternalsHelper.IsInternalToDynamicProxy(target.Assembly);
            var isAccessible = target.IsPublic || target.IsNestedPublic || internalAndVisibleToDynProxy;

            if (!isAccessible)
            {
                throw new GeneratorException(String.Format(CultureInfo.CurrentUICulture, Resources.DynamicProxy_InterfaceTypeToProxyNotPublic, target.FullName));
            }
            if (target.IsGenericTypeDefinition)
            {
                throw new GeneratorException(String.Format(CultureInfo.CurrentUICulture, Resources.DynamicProxy_InterfaceTypeToProxyIsGeneric, target.FullName));
            }
        }
예제 #10
0
    internal static SyntaxTree GetSyntaxTree(Query query)
    {
        if (query != null)
        {
            switch (query.Language)
            {
            case QueryLanguage.Expression:
                return(Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree.ParseText(query.Text,
                                                                                InternalsHelper.GetCSharpParseOptionsForSourceCodeKind(SourceCodeKind.Interactive)));

            case QueryLanguage.Statements:
                return(Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree.ParseText(query.Text,
                                                                                InternalsHelper.GetCSharpParseOptionsForSourceCodeKind(SourceCodeKind.Script)));

            case QueryLanguage.Program:
                return(Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree.ParseText(query.Text,
                                                                                InternalsHelper.GetCSharpParseOptionsForSourceCodeKind(SourceCodeKind.Script)));

            case QueryLanguage.VBExpression:
                return(Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxTree.ParseText(query.Text,
                                                                                          InternalsHelper.GetVisualBasicParseOptionsForSourceCodeKind(SourceCodeKind.Interactive)));

            case QueryLanguage.VBStatements:
                return(Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxTree.ParseText(query.Text,
                                                                                          InternalsHelper.GetVisualBasicParseOptionsForSourceCodeKind(SourceCodeKind.Script)));

            case QueryLanguage.VBProgram:
                return(Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxTree.ParseText(query.Text,
                                                                                          InternalsHelper.GetVisualBasicParseOptionsForSourceCodeKind(SourceCodeKind.Script)));

            default:
                return(null);
            }
        }
        return(null);
    }