private static DynamicCtorDelegate GetCachedConstructor(Type objectType)
        {
            DynamicCtorDelegate result = null;

            if (!_ctorCache.TryGetValue(objectType, out result))
            {
                lock (_ctorCache)
                {
                    if (!_ctorCache.TryGetValue(objectType, out result))
                    {
                        ConstructorInfo info = objectType.GetConstructor(ctorFlags, null, Type.EmptyTypes, null);
                        if (info == null)
                        {
                            throw new NotSupportedException(string.Format(
                                                                CultureInfo.CurrentCulture,
                                                                "Cannot create instance of Type '{0}'. No public parameterless constructor found.",
                                                                objectType));
                        }

                        result = DynamicMethodHandlerFactory.CreateConstructor(info);
                        _ctorCache.Add(objectType, result);
                    }
                }
            }
            return(result);
        }
示例#2
0
        private static DynamicCtorDelegate GetCachedConstructor(Type objectType)
        {
            DynamicCtorDelegate result = null;
            var found = false;

            try
            {
                found = _ctorCache.TryGetValue(objectType, out result);
            }
            catch
            { /* failure will drop into !found block */ }
            if (!found)
            {
                lock (_ctorCache)
                {
                    if (!_ctorCache.TryGetValue(objectType, out result))
                    {
#if NETFX_CORE
                        ConstructorInfo info = objectType.GetConstructor(ctorFlags, null, new Type[] { }, null);
#else
                        ConstructorInfo info = objectType.GetConstructor(ctorFlags, null, Type.EmptyTypes, null);
#endif
                        if (info == null)
                        {
                            throw new NotSupportedException(string.Format(
                                                                CultureInfo.CurrentCulture,
                                                                "Cannot create instance of Type '{0}'. No public parameterless constructor found.",
                                                                objectType));
                        }

                        result = DynamicMethodHandlerFactory.CreateConstructor(info);
                        _ctorCache.Add(objectType, result);
                    }
                }
            }
            return(result);
        }