DeftTech.DuckTyping.DelegateDuckProxyType.CanProxy C# (CSharp) Method

CanProxy() public method

Determines whether a proxy can be generated for the types given in the constructor.
public CanProxy ( ) : bool
return bool
        public bool CanProxy()
        {
            bool canProxy = false;

            if (m_ToDelegateType.IsSubclassOf(typeof(Delegate)) && m_FromDelegateType.IsSubclassOf(typeof(Delegate)))
            {
                MethodInfo variantMethod = m_ToDelegateType.GetMethod("Invoke");
                MethodInfo duckMethod = m_FromDelegateType.GetMethod("Invoke");

                // Must have a compatible parameter list
                ParameterInfo[] variantMethodParameters = variantMethod.GetParameters();
                ParameterInfo[] duckMethodParameters = duckMethod.GetParameters();

                if (duckMethodParameters.Length == variantMethodParameters.Length)
                {
                    bool areParameterTypesCompatible = true;

                    for (int i = 0; i < duckMethodParameters.Length; i++)
                    {
                        if (!DuckTyping.AreTypesCompatible(variantMethodParameters[i].ParameterType, duckMethodParameters[i].ParameterType))
                        {
                            areParameterTypesCompatible = false;
                            break;
                        }
                    }

                    if (areParameterTypesCompatible)
                    {
                        // Must have a compatible return type
                        if (DuckTyping.AreTypesCompatible(variantMethod.ReturnType, duckMethod.ReturnType))
                        {
                            canProxy = true;
                        }
                    }
                }
            }

            return canProxy;
        }

Usage Example

Exemplo n.º 1
0
        /// <summary>
        /// Gets the DelegateDuckProxyType object for a given duck type.
        /// </summary>
        /// <param name="toType">Type to be casted to.</param>
        /// <param name="fromType">Type of delegate to be casted.</param>
        /// <returns>The duck proxy type to use to cast or prepare for casting.</returns>
        private static DelegateDuckProxyType GetProxyType(Type toType, Type fromType)
        {
            DelegateDuckProxyType proxyType = null;

            FromTypeTree <DelegateDuckProxyType> fromTypeTree = null;

            if (s_ProxyTypeTree.ContainsKey(toType))
            {
                fromTypeTree = s_ProxyTypeTree[toType];

                if (fromTypeTree.ContainsKey(fromType))
                {
                    proxyType = fromTypeTree[fromType];
                }
            }

            if (proxyType == null)
            {
                proxyType = new DelegateDuckProxyType(toType, fromType);

                if (proxyType.CanProxy())
                {
                    if (fromTypeTree == null)
                    {
                        fromTypeTree = new FromTypeTree <DelegateDuckProxyType>();
                        s_ProxyTypeTree.Add(toType, fromTypeTree);
                    }

                    fromTypeTree.Add(fromType, proxyType);
                }
            }

            return(proxyType);
        }
All Usage Examples Of DeftTech.DuckTyping.DelegateDuckProxyType::CanProxy