Example #1
0
		private void WriteCustomAttributes(XmlWriter writer, ConstructorInfo constructorInfo)
		{
			try
			{
				WriteCustomAttributes(writer, constructorInfo.GetCustomAttributes(this.rep.DocumentInheritedAttributes), "");
			}
			catch (Exception e)
			{
				TraceErrorOutput("Error retrieving custom attributes for " + MemberID.GetMemberID(constructorInfo), e);
			}
		}
Example #2
0
        /// <summary>
        /// Formats the constructor.
        /// </summary>
        /// <param name="constructor">The constructor.</param>
        /// <param name="sw">The string writer.</param>
        private static void FormatConstructor(ConstructorInfo constructor, StringWriter sw)
        {
            foreach (Attribute attribute in constructor.GetCustomAttributes(false))
            {
                FormatAttribute(sw, attribute);
            }

            sw.Write(constructor.DeclaringType.GetTypeInfo().Name);
            sw.Write("(");
            foreach (var parameterInfo in constructor.GetParameters())
            {
                foreach (Attribute attribute in parameterInfo.GetCustomAttributes(false))
                {
                    FormatAttribute(sw, attribute);
                }

                sw.Write(parameterInfo.ParameterType.Format());
                sw.Write(" ");
                sw.Write(parameterInfo.Name);
            }

            sw.WriteLine(")");
        }
Example #3
0
		public static bool IsConstructable( ConstructorInfo ctor, AccessLevel accessLevel )
		{
			object[] attrs = ctor.GetCustomAttributes( m_ConstructableType, false );

			if ( attrs.Length == 0 )
				return false;

			return accessLevel >= ((ConstructableAttribute)attrs[0]).AccessLevel;
		}
            public Expression GetConstructorExpression(out ConstructorInfo constructorInfo, out IEnumerable<Expression> constructorParameters)
            {
                CompiledExportDelegateInfo exportDelegateInfo = _instanceCompiledExport.exportDelegateInfo;

                constructorInfo = _instanceCompiledExport.exportDelegateInfo.ImportConstructor ??
                                  _instanceCompiledExport.PickConstructor(exportDelegateInfo.ActivationType);

                if (constructorInfo == null)
                {
                    throw new PublicConstructorNotFoundException(exportDelegateInfo.ActivationType);
                }

                List<Expression> parameters = new List<Expression>();
                constructorParameters = parameters;

                Attribute[] constructorAttributes = constructorInfo.GetCustomAttributes(true).ToArray();

                if (constructorAttributes.Length == 0)
                {
                    constructorAttributes = EmptyAttributesArray;
                }

                foreach (ParameterInfo parameterInfo in constructorInfo.GetParameters())
                {
                    Attribute[] parameterAttributes = parameterInfo.GetCustomAttributes(true).ToArray();

                    if (parameterAttributes.Length == 0)
                    {
                        parameterAttributes = EmptyAttributesArray;
                    }

                    IExportValueProvider valueProvider = null;
                    ExportStrategyFilter exportStrategyFilter = null;
                    string importName = null;
                    object comparerObject = null;
                    ILocateKeyValueProvider locateKey = null;

                    if (exportDelegateInfo.ConstructorParams != null)
                    {
                        foreach (ConstructorParamInfo constructorParamInfo in exportDelegateInfo.ConstructorParams)
                        {
                            if (string.Compare(parameterInfo.Name,
                                constructorParamInfo.ParameterName,
                                StringComparison.OrdinalIgnoreCase) == 0)
                            {
                                importName = constructorParamInfo.ImportName;
                                exportStrategyFilter = constructorParamInfo.ExportStrategyFilter;
                                valueProvider = constructorParamInfo.ValueProvider;
                                comparerObject = constructorParamInfo.ComparerObject;
                                locateKey = constructorParamInfo.LocateKeyProvider;
                                break;
                            }
                        }

                        if (valueProvider == null)
                        {
                            foreach (ConstructorParamInfo constructorParamInfo in exportDelegateInfo.ConstructorParams)
                            {
                                if (string.IsNullOrEmpty(constructorParamInfo.ParameterName) &&
                                    parameterInfo.ParameterType.GetTypeInfo().IsAssignableFrom(
                                        constructorParamInfo.ParameterType.GetTypeInfo()))
                                {
                                    importName = constructorParamInfo.ImportName;
                                    exportStrategyFilter = constructorParamInfo.ExportStrategyFilter;
                                    valueProvider = constructorParamInfo.ValueProvider;
                                    comparerObject = constructorParamInfo.ComparerObject;
                                    locateKey = constructorParamInfo.LocateKeyProvider;
                                    break;
                                }
                            }
                        }
                    }

                    InjectionTargetInfo targetInfo = null;

                    if (importName != null)
                    {
                        targetInfo = new InjectionTargetInfo(exportDelegateInfo.ActivationType,
                            _instanceCompiledExport.activationTypeAttributes,
                            parameterInfo,
                            parameterAttributes,
                            constructorAttributes,
                            importName,
                            null);
                    }
                    else if (InjectionKernel.ImportTypeByName(parameterInfo.ParameterType))
                    {
                        targetInfo = new InjectionTargetInfo(exportDelegateInfo.ActivationType,
                            _instanceCompiledExport.activationTypeAttributes,
                            parameterInfo,
                            parameterAttributes,
                            constructorAttributes,
                            parameterInfo.Name,
                            null);
                    }
                    else
                    {
                        targetInfo = new InjectionTargetInfo(exportDelegateInfo.ActivationType,
                            _instanceCompiledExport.activationTypeAttributes,
                            parameterInfo,
                            parameterAttributes,
                            constructorAttributes,
                            null,
                            parameterInfo.ParameterType);
                    }

                    ParameterExpression parameterExpression =
                            _instanceCompiledExport.CreateImportExpression(parameterInfo.ParameterType,
                            targetInfo,
                            ExportStrategyDependencyType.ConstructorParameter,
                            importName,
                            parameterInfo.Name + "CVar",
                            true,
                            valueProvider,
                            exportStrategyFilter,
                            locateKey,
                            comparerObject,
                            null);

                    parameters.Add(Expression.Convert(parameterExpression, parameterInfo.ParameterType));
                }

                Expression constructExpression = Expression.New(constructorInfo, parameters.ToArray());

                return Expression.Assign(_instanceCompiledExport.instanceVariable, constructExpression);
            }
 public static T GetCustomAttribute <T>(this ConstructorInfo type, bool inherit)
     where T : Attribute
 {
     return(type.GetCustomAttributes(inherit).OfType <T>().FirstOrDefault());
 }
Example #6
0
        public Constructor(ConstructorInfo constructorInfo, Dictionary<string, object> parameterDictionary)
        {
            _constructorInfo = constructorInfo;
            _parameterNames = new List<string>();
            _parameterTypes = new List<Type>();
            _match = true;
            _usedAllParameters = true;

            // look through the constructor attributes for a RegisterConstructorAttribute
            // if this attribute exists, the factory is allowed to use the constructor.
            var registerConstructorAttributes = (RegisterConstructorAttribute[])_constructorInfo.GetCustomAttributes(typeof(RegisterConstructorAttribute), false);
            if (registerConstructorAttributes.Length == 0)
            {
                _match = false;
                _usedAllParameters = false;
                return;
            }

            // populate _parameterNames and _parameterTypes from _constructorInfo
            ParameterInfo[] parameterInfos = _constructorInfo.GetParameters();
            foreach (ParameterInfo parameterInfo in parameterInfos)
            {
                var factoryParameterAttributes = (FactoryParameterAttribute[])parameterInfo.GetCustomAttributes(typeof(FactoryParameterAttribute),false);

                string parameterName = parameterInfo.Name.ToLower(); // use raw parameter name
                if (factoryParameterAttributes.Length == 1) parameterName = factoryParameterAttributes[0].RegisteredName; // replace with friendly name for the parameter.
                else if (factoryParameterAttributes.Length > 1) throw new Exception("Parameter can have at most one FactoryParameterAttribute");
                _parameterNames.Add(parameterName);
                _parameterTypes.Add(parameterInfo.ParameterType);
            }

            // populate _parameterValues from parameterDictionary
            _parameterValues = new object[_parameterNames.Count];
            for (int i = 0; i < _parameterNames.Count; ++i)
            {
                string parameterName = _parameterNames[i].ToLower();
                Type parameterType = _parameterTypes[i];
                object parameterValue;
                if (parameterDictionary.ContainsKey(parameterName))
                {
                    parameterValue = parameterDictionary[parameterName];
                    if (!parameterType.IsInstanceOfType(parameterValue)) // Parameter parameterName has the wrong type
                    {
                        if (parameterValue.GetType() == typeof(Dictionary<string, object>))
                        {
                            var dictionary = (Dictionary<string, object>) parameterValue;
                            MethodInfo factoryGetInstanceMethod = typeof(Factory).GetMethod("GetInstance").MakeGenericMethod(parameterType);
                            try
                            {
                                parameterValue = factoryGetInstanceMethod.Invoke(null, new object[] { dictionary });
                            }
                            catch (TargetInvocationException e)
                            {
                                throw new Exception(e.InnerException.Message);
                            }
                        }
                        else
                            _match = false;
                    }
                }
                else // Can't find parameter parameterName in parameterDictionary
                {
                    _match = false;
                    parameterValue = new object();
                }
                _parameterValues[i] = parameterValue;
            }

            // check to see whether all parameters in parameterDictionary have been used up
            foreach (KeyValuePair<string, object> entry in parameterDictionary.Where(entry => entry.Key != "name" && !_parameterNames.Contains(entry.Key)))
                _usedAllParameters = false;
        }
 private bool HasInjectAttribute(ConstructorInfo constructor)
 {
     return constructor.GetCustomAttributes(false).Any(a => a.GetType().Name.ToLower().Contains("inject"));
 }
Example #8
0
		public static CAL GetCAL(ConstructorInfo ctor)
		{
			object[] attrs = ctor.GetCustomAttributes(m_ConstructableAccessLevel, false);

			if (attrs.Length == 0)
				return null;

			return attrs[0] as CAL;
		}
Example #9
0
 public void PublicConstructor_IsTaggedWithPublicAPIAttribute(ConstructorInfo constructor)
 {
     bool attributeIsPresent = constructor.GetCustomAttributes(typeof(PublicAPIAttribute), true).Any();
     Assume.That(constructor.DeclaringType != null);
     Assert.That(attributeIsPresent, "Constructor (" + string.Join(", ", constructor.GetParameters().Select(p => p.ParameterType.Name)) + " of type " + constructor.DeclaringType.Name + " is not tagged with [PublicAPI]");
 }
        public static ExcelObjectConstructorAttribute GetAttribute(ConstructorInfo ci)
        {
            object[] o = ci.GetCustomAttributes(typeof(ExcelObjectConstructorAttribute), false);
            ExcelObjectConstructorAttribute r = (ExcelObjectConstructorAttribute)o[0];

            return r;
        }
Example #11
0
 private static ConstructorDefinition BuildConstructorDefinition(ConstructorInfo constructorInfo)
 {
     IEnumerable<ParameterDefinition> parameters = constructorInfo
         .GetParameters()
         .Select(p => new ParameterDefinition(p.Name, p.ParameterType));
     ConstructorMethod constructorMethod = ObjectInterfaceProvider.GetConstructor(constructorInfo);
     bool isPreferredConstructor = constructorInfo.GetCustomAttributes(typeof(SerializationConstructorAttribute), true).Any();
     return new ConstructorDefinition(constructorMethod, parameters, isPreferredConstructor);
 }
Example #12
0
 public static T[] GetAttributes <T>(ConstructorInfo ci, bool inherit) where T : Attribute
 {
     object[] objects = ci.GetCustomAttributes(typeof(T), inherit);
     return((T[])objects);
 }