System.Dynamic.Utils.TypeUtils.CanCache C# (CSharp) Method

CanCache() public static method

We can cache references to types, as long as they aren't in collectible assemblies. Unfortunately, we can't really distinguish between different flavors of assemblies. But, we can at least create a cache for types in mscorlib (so we get the primitives, etc).
public static CanCache ( this t ) : bool
t this
return bool
        public static bool CanCache(this Type t)
        {
            // Note: we don't have to scan base or declaring types here.
            // There's no way for a type in mscorlib to derive from or be
            // contained in a type from another assembly. The only thing we
            // need to look at is the generic arguments, which are the thing
            // that allows mscorlib types to be specialized by types in other
            // assemblies.

            Assembly asm = t.GetTypeInfo().Assembly;
            if (asm != _mscorlib)
            {
                // Not in mscorlib or our assembly
                return false;
            }

            if (t.GetTypeInfo().IsGenericType)
            {
                foreach (Type g in t.GetGenericArguments())
                {
                    if (!g.CanCache())
                    {
                        return false;
                    }
                }
            }

            return true;
        }
    }

Usage Example

Esempio n. 1
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);
        }
All Usage Examples Of System.Dynamic.Utils.TypeUtils::CanCache