Beispiel #1
0
        /// <summary>
        /// Creates new instance of the type defined by this node.
        /// </summary>
        /// <param name="context">Context to evaluate expressions against.</param>
        /// <param name="evalContext">Current expression evaluation context.</param>
        /// <returns>Node's value.</returns>
        protected override object Get(object context, EvaluationContext evalContext)
        {
            object[] argValues = ResolveArguments(evalContext);
            IDictionary namedArgValues = ResolveNamedArguments(evalContext);

            if (constructor == null)
            {
                lock(this)
                {
                    if (constructor == null)
                    {
                        constructor = InitializeNode(argValues, namedArgValues);
                    }
                }
            }

            object[] paramValues = (isParamArray ? ReflectionUtils.PackageParamArray(argValues, argumentCount, paramArrayType) : argValues);
            object instance = constructor.Invoke(paramValues);
            if (namedArgValues != null)
            {
                SetNamedArguments(instance, namedArgValues);
            }
            
            return instance;
        }
        private static CreateControlCollectionDelegate GetInterceptedCollectionFactory(Type controlType, Type controlCollectionType)
        {
            AssertUtils.State( typeof(Control).IsAssignableFrom(controlType), "controlType must be of type Control" );
            AssertUtils.State( typeof(ControlCollection).IsAssignableFrom(controlCollectionType), "controlCollectionType must be of type ControlCollection" );

            CreateControlCollectionDelegate factoryMethod = (CreateControlCollectionDelegate)s_collectionFactoryCache[controlType];
            if (factoryMethod == null)
            {
                lock (s_collectionFactoryCache)
                {
                    factoryMethod = (CreateControlCollectionDelegate)s_collectionFactoryCache[controlType];
                    if (factoryMethod == null)
                    {
                        Type interceptedCollectionType = GetInterceptedCollectionType(controlCollectionType, new InjectDependenciesCallbackHandler(WebDependencyInjectionUtils.InjectDependenciesRecursive));

                        ConstructorInfo ctor = interceptedCollectionType.GetConstructor(new Type[] {typeof (Control)});
                        IDynamicConstructor dynCtor = new SafeConstructor(ctor);
                        s_collectionFactoryCache[controlType] = new CreateControlCollectionDelegate(new CreateControlCollectionWrapper(dynCtor).Create);
                    }
                }
            }
            return factoryMethod;
        }
Beispiel #3
0
        /// <summary>
        /// Initializes this node by caching necessary constructor and property info.
        /// </summary>
        /// <param name="argValues"></param>
        /// <param name="namedArgValues"></param>
        private SafeConstructor InitializeNode(object[] argValues, IDictionary namedArgValues)
        {
            SafeConstructor ctor = null;
            Type objectType = GetObjectType(this.getText().Trim());
                
            // cache constructor info
            ConstructorInfo ci = GetBestConstructor(objectType, argValues);
            if (ci == null)
            {
                throw new ArgumentException(
                    String.Format("Constructor for the type [{0}] with a specified " +
                                  "number and types of arguments does not exist.",
                                  objectType.FullName));
            }
            else 
            {
                ParameterInfo[] parameters = ci.GetParameters();
                if (parameters.Length > 0)
                {
                    ParameterInfo lastParameter = parameters[parameters.Length - 1];
                    isParamArray = lastParameter.GetCustomAttributes(typeof(ParamArrayAttribute), false).Length > 0;
                    if (isParamArray)
                    {
                        paramArrayType = lastParameter.ParameterType.GetElementType();
                        argumentCount = parameters.Length;
                    }
                }
                ctor = new SafeConstructor(ci);
            }
                
            // cache named args info
            if (namedArgValues != null)
            {
                namedArgs = new Hashtable(namedArgValues.Count);
                foreach (string name in namedArgValues.Keys)
                {
                    this.namedArgs[name] = Expression.ParseProperty(name);
                }
            }

            return ctor;
        }
Beispiel #4
0
 public ASTNodeCreator(ConstructorInfo ctor)
 {
     this.ctor = new SafeConstructor(ctor);
     this.name = ctor.DeclaringType.FullName;
 }