Example #1
0
        protected override void RenderGenericConstructor(GenericConstructor genericConstructor)
        {
            if (genericConstructor == null)
            {
                return;
            }
            var args = string.Join(", ", genericConstructor.Arguments.Select(a => RenderGenericVariableDefinition(a)));

            WriteIndented($"{genericConstructor.AccessModifier.ToString().ToLower()} {genericConstructor.Name}({args})");
            WriteIndented("{");
            _indentLevel++;
            WriteIndented(genericConstructor.Body);
            _indentLevel--;
            WriteIndented("}");
        }
Example #2
0
        internal object CreateInstance(Type t)
        {
#if SILVERLIGHT || (!DotNet && !DotNetStandard)
            // Falling back to default parameterless constructor.
            return(Activator.CreateInstance(t, null));
#else
            // Read from cache
#if SILVERLIGHT || (!DotNet && !DotNetStandard)
            GenericConstructor info = null;
            _constructorCache.TryGetValue(t, out info);
#else
            var info = _constructorCache[t] as GenericConstructor;
#endif
            if (info == null)
            {
                ConstructorInfo ctor = t.GetConstructor(Type.EmptyTypes);
                if (ctor == null)
                {
                    // Falling back to default parameterless constructor.
                    return(Activator.CreateInstance(t, null));
                }

#if SILVERLIGHT || (!DotNet && !DotNetStandard)
                var dynamicCtor = new DynamicMethod("_", t, Type.EmptyTypes);
#else
                var dynamicCtor = new DynamicMethod("_", t, Type.EmptyTypes, t, true);
#endif

                var il = dynamicCtor.GetILGenerator();

                il.Emit(OpCodes.Newobj, ctor);
                il.Emit(OpCodes.Ret);

                info = (GenericConstructor)dynamicCtor.CreateDelegate(typeof(GenericConstructor));

                _constructorCache[t] = info;
            }
            if (info == null)
            {
                throw new MissingMethodException(string.Format("No parameterless constructor defined for '{0}'.", t));
            }
            return(info.Invoke());
#endif
        }
Example #3
0
 protected abstract void RenderGenericConstructor(GenericConstructor genericConstructor);