示例#1
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, nameof(delegateType));
            ContractUtils.RequiresNotNull(binder, nameof(binder));
            if (!delegateType.IsSubclassOf(typeof(MulticastDelegate)))
            {
                throw new ArgumentException("Type must be derived from System.Delegate");
            }

            var constructors = _siteConstructors;

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

            if (constructors.TryGetValue(delegateType, out var ctor))
            {
                return(ctor(binder));
            }

            var method = typeof(CallSite <>).MakeGenericType(delegateType).GetMethod(nameof(Create));

            ctor = (Func <CallSiteBinder, CallSite>)method.CreateDelegate(typeof(Func <CallSiteBinder, CallSite>));
            constructors.Add(delegateType, ctor);
            return(ctor(binder));
        }
        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
        /// <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, nameof(delegateType));
            ContractUtils.RequiresNotNull(binder, nameof(binder));
            if (!delegateType.IsSubclassOf(typeof(MulticastDelegate)))
            {
                throw System.Linq.Expressions.Error.TypeMustBeDerivedFromSystemDelegate();
            }

            CacheDict <Type, Func <CallSiteBinder, CallSite> >?ctors = s_siteCtors;

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

            if (!ctors.TryGetValue(delegateType, out Func <CallSiteBinder, CallSite>?ctor))
            {
                MethodInfo method = typeof(CallSite <>).MakeGenericType(delegateType).GetMethod(nameof(Create)) !;

                if (delegateType.IsCollectible)
                {
                    // slow path
                    return((CallSite)method.Invoke(null, new object[] { binder }) !);
                }

                ctor = (Func <CallSiteBinder, CallSite>)method.CreateDelegate(typeof(Func <CallSiteBinder, CallSite>));
                ctors.Add(delegateType, ctor);
            }

            return(ctor(binder));
        }
 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 });
 }
示例#5
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);
        }
示例#6
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");
            if (!delegateType.IsSubclassOf(typeof(MulticastDelegate)))
            {
                throw Error.TypeMustBeDerivedFromSystemDelegate();
            }

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

            MethodInfo method = null;
            var        ctors  = s_siteCtors;

            lock (ctors)
            {
                if (!ctors.TryGetValue(delegateType, out ctor))
                {
                    method = typeof(CallSite <>).MakeGenericType(delegateType).GetMethod("Create");

                    if (TypeUtils.CanCache(delegateType))
                    {
                        ctor = (Func <CallSiteBinder, CallSite>)method.CreateDelegate(typeof(Func <CallSiteBinder, CallSite>));
                        ctors.Add(delegateType, ctor);
                    }
                }
            }
            if (ctor != null)
            {
                return(ctor(binder));
            }

            // slow path
            return((CallSite)method.Invoke(null, new object[] { binder }));
        }
示例#7
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));
        }
示例#8
0
        /// <summary>
        /// Adds a new element to the cache, replacing and moving it to the front if the
        /// element is already present.
        /// </summary>
        internal void Add(TKey key, TValue value)
        {
            KeyInfo keyInfo;

            if (_dict.TryGetValue(key, out keyInfo))
            {
                // remove original entry from the linked list
                _list.Remove(keyInfo.List);
            }
            else if (_list.Count == _maxSize)
            {
                // we've reached capacity, remove the last used element...
                LinkedListNode <TKey> node = _list.Last;
                _list.RemoveLast();
                bool res = _dict.Remove(node.Value);
                Debug.Assert(res);
            }

            // add the new entry to the head of the list and into the dictionary
            LinkedListNode <TKey> listNode = new LinkedListNode <TKey>(key);

            _list.AddFirst(listNode);
            _dict[key] = new CacheDict <TKey, TValue> .KeyInfo(value, listNode);
        }
示例#9
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;

            var ctors = _SiteCtors;
            lock (ctors) {
                if (!ctors.TryGetValue(delegateType, out ctor)) {
                    MethodInfo method = typeof(CallSite<>).MakeGenericType(delegateType).GetMethod("Create");
                    ctor = (Func<CallSiteBinder, CallSite>)Delegate.CreateDelegate(typeof(Func<CallSiteBinder, CallSite>), method);
                    ctors.Add(delegateType, ctor);
                }
            }
            return ctor(binder);
        }
示例#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");
            if (!delegateType.IsSubclassOf(typeof(MulticastDelegate))) throw Error.TypeMustBeDerivedFromSystemDelegate();

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

            Func<CallSiteBinder, CallSite> ctor;
            MethodInfo method = null;
            if (!ctors.TryGetValue(delegateType, out ctor))
            {
                method = typeof(CallSite<>).MakeGenericType(delegateType).GetMethod("Create");

                if (TypeUtils.CanCache(delegateType))
                {
                    ctor = (Func<CallSiteBinder, CallSite>)method.CreateDelegate(typeof(Func<CallSiteBinder, CallSite>));
                    ctors.Add(delegateType, ctor);
                }
            }

            if (ctor != null)
            {
                return ctor(binder);
            }

            // slow path
            return (CallSite)method.Invoke(null, new object[] { binder });
        }
示例#11
0
 public static void _clearcache() {
     _cache = new CacheDict<string, Struct>(MAX_CACHE_SIZE);
 }
示例#12
0
 static HackExtensions()
 {
     Mscorlib       = typeof(object).Assembly;
     SystemCore     = typeof(Expression).Assembly;
     ParamInfoCache = new CacheDict <MethodBase, ParameterInfo[]>(0x4b);
 }