示例#1
0
            protected internal override Expression VisitConstant(ConstantExpression node)
            {
                int index = _constants.IndexOf(node);

                if (index != -1)
                {
                    // clear the constant from the list...  This is incase the rule contains the
                    // same ConstantExpression instance multiple times and we're replacing the
                    // multiple entries.  In that case we want each constant duplicated value
                    // to line up with a single index.
                    _constants[index] = null;

                    // this is a constant we want to re-write, replace w/ a templated constant
                    object value = node.Value;

                    Type elementType = TypeUtils.GetConstantType(node.Type);

                    Func <object, int, object> ctor;
                    if (!_templateCtors.TryGetValue(elementType, out ctor))
                    {
                        MethodInfo genMethod = typeof(TemplatedValue <>).MakeGenericType(new Type[] { elementType }).GetMethod("Make", BindingFlags.NonPublic | BindingFlags.Static);
                        _templateCtors[elementType] = ctor = (Func <object, int, object>)genMethod.CreateDelegate(typeof(Func <object, int, object>));;
                    }

                    object constVal = ctor(value, index);

                    return(Expression.Property(Expression.Constant(constVal), ctor.Method.ReturnType.GetProperty("Value")));
                }

                return(base.VisitConstant(node));
            }
        public static CallSite Create(Type delegateType, CallSiteBinder binder)
        {
            Func <CallSiteBinder, CallSite> func;

            ContractUtils.RequiresNotNull(delegateType, "delegateType");
            ContractUtils.RequiresNotNull(binder, "binder");
            if (!delegateType.IsSubclassOf(typeof(Delegate)))
            {
                throw Error.TypeMustBeDerivedFromSystemDelegate();
            }
            if (_SiteCtors == null)
            {
                _SiteCtors = new CacheDict <Type, Func <CallSiteBinder, CallSite> >(100);
            }
            MethodInfo method = null;
            CacheDict <Type, Func <CallSiteBinder, CallSite> > dict = _SiteCtors;

            lock (dict)
            {
                if (!dict.TryGetValue(delegateType, out func))
                {
                    method = typeof(CallSite <>).MakeGenericType(new Type[] { delegateType }).GetMethod("Create");
                    if (delegateType.CanCache())
                    {
                        func = (Func <CallSiteBinder, CallSite>)Delegate.CreateDelegate(typeof(Func <CallSiteBinder, CallSite>), method);
                        dict.Add(delegateType, func);
                    }
                }
            }
            if (func != null)
            {
                return(func(binder));
            }
            return((CallSite)method.Invoke(null, new object[] { binder }));
        }
示例#3
0
 private static MethodInfo GetRunMethodOrFastCtor(Type delegateType, out Func <LightLambda, Delegate> fastCtor)
 {
     lock (_runCache) {
         if (_runCache.TryGetValue(delegateType, out fastCtor))
         {
             return(null);
         }
         return(MakeRunMethodOrFastCtor(delegateType, out fastCtor));
     }
 }
 private static Func <LightLambda, Delegate> GetRunDelegateCtor(Type delegateType)
 {
     lock (_runCache) {
         if (_runCache.TryGetValue(delegateType, out Func <LightLambda, Delegate> fastCtor))
         {
             return(fastCtor);
         }
         return(MakeRunDelegateCtor(delegateType));
     }
 }
示例#5
0
        private static bool IsValueTypeRecursiveExtracted(Type type)
        {
            var info = type.GetTypeInfo();

            if (!info.IsValueType)
            {
                return(false);
            }
            if (_valueTypeRecursiveCache.TryGetValue(type, out var result))
            {
                return(result);
            }
            result = GetValueTypeRecursiveResult(type);
            _binaryPortableCache[type] = result;
            return(result);
        }
示例#6
0
        public static T[] Empty <T>()
        {
            var type = typeof(T);

            if (type == typeof(Type))
            {
                return((T[])(object)TypeEx.EmptyTypes);
            }
            if (_emptyArrays.TryGetValue(type, out var array))
            {
                return((T[])array);
            }
            var result = new T[0];

            _emptyArrays[type] = result;
            return(result);
        }
示例#7
0
        internal static ParameterInfo[] GetParametersCached(this MethodBase method)
        {
            CacheDict <MethodBase, ParameterInfo[]> paramInfoCache = TypeExtensions._ParamInfoCache;

            ParameterInfo[] parameters;
            if (!paramInfoCache.TryGetValue(method, out parameters))
            {
                parameters = method.GetParameters();
                Type declaringType = method.DeclaringType;
                if (declaringType != (Type)null && declaringType.CanCache())
                {
                    paramInfoCache[method] = parameters;
                }
            }

            return(parameters);
        }
示例#8
0
        internal static ParameterInfo[] GetParametersCached(this MethodBase method)
        {
            ParameterInfo[] pis;
            lock (_ParamInfoCache) {
                if (!_ParamInfoCache.TryGetValue(method, out pis))
                {
                    pis = method.GetParameters();

                    Type t = method.DeclaringType;
                    if (t != null && TypeUtils.CanCache(t))
                    {
                        _ParamInfoCache[method] = pis;
                    }
                }
            }
            return(pis);
        }
示例#9
0
 internal static ParameterInfo[] GetParametersCached(this MethodBase method)
 {
     ParameterInfo[] parameters = null;
     lock (ParamInfoCache)
     {
         if (!ParamInfoCache.TryGetValue(method, out parameters))
         {
             parameters = method.GetParameters();
             Type declaringType = method.DeclaringType;
             if ((declaringType != null) && declaringType.CanCache())
             {
                 ParamInfoCache[method] = parameters;
             }
         }
     }
     return(parameters);
 }
示例#10
0
        /// <summary>
        /// Creates a CallSite with the given delegate type and binder.
        /// </summary>
        /// <param name="delegateType">The CallSite delegate type.</param>
        /// <param name="binder">The CallSite binder.</param>
        /// <returns>The new CallSite.</returns>
        public static CallSite Create(Type delegateType, CallSiteBinder binder)
        {
            ContractUtils.RequiresNotNull(delegateType, "delegateType");
            ContractUtils.RequiresNotNull(binder, "binder");
            ContractUtils.Requires(delegateType.IsSubclassOf(typeof(Delegate)), "delegateType", Strings.TypeMustBeDerivedFromSystemDelegate);

            if (_SiteCtors == null)
            {
                // It's okay to just set this, worst case we're just throwing away some data
                _SiteCtors = new CacheDict <Type, Func <CallSiteBinder, CallSite> >(100);
            }
            Func <CallSiteBinder, CallSite> ctor;

            lock (_SiteCtors) {
                if (!_SiteCtors.TryGetValue(delegateType, out ctor))
                {
                    MethodInfo method = typeof(CallSite <>).MakeGenericType(delegateType).GetMethod("Create");
                    ctor = (Func <CallSiteBinder, CallSite>)Delegate.CreateDelegate(typeof(Func <CallSiteBinder, CallSite>), method);
                    _SiteCtors.Add(delegateType, ctor);
                }
            }
            return(ctor(binder));
        }