/// <summary> /// Converts constant values from a foreign form to a native one. /// </summary> /// <param name="value">The foreign constant value.</param> /// <param name="typeConstructor">A function for resolving types.</param> /// <returns>The native constant value.</returns> protected static ConstantValue ConvertConstantValue <TType>(object value, Common.Func <TType, ITypeInfo> typeConstructor) where TType : class { if (value == null) { return(new ConstantValue(Reflector.Wrap(typeof(object)), null)); } if (value != null) { TType type = value as TType; if (type != null) { return(new ConstantValue(Reflector.Wrap(typeof(Type)), typeConstructor(type))); } Type valueType = value.GetType(); if (valueType.IsArray) { Array valueArray = (Array)value; int length = valueArray.Length; ConstantValue[] array = new ConstantValue[length]; for (int i = 0; i < length; i++) { array[i] = ConvertConstantValue(valueArray.GetValue(i), typeConstructor); } return(new ConstantValue(Reflector.Wrap(MapConstantArrayElementType <TType>(valueType).MakeArrayType()), array)); } } return(new ConstantValue(Reflector.Wrap(value.GetType()), value)); }
protected override ConstantValue[] GetAttributeConstructorArguments(StaticAttributeWrapper attribute) { IAttributeInstance attributeHandle = (IAttributeInstance)attribute.Handle; IList<IParameter> parameters = attributeHandle.Constructor.Parameters; if (parameters.Count == 0) return EmptyArray<ConstantValue>.Instance; List<ConstantValue> values = new List<ConstantValue>(); for (int i = 0; ; i++) { #if RESHARPER_50_OR_NEWER if (i == attributeHandle.PositionParameterCount) break; #endif ConstantValue? value = GetAttributePositionParameter(attributeHandle, i); if (value.HasValue) values.Add(value.Value); else break; } int lastParameterIndex = parameters.Count - 1; IParameter lastParameter = parameters[lastParameterIndex]; if (!lastParameter.IsParameterArray) return values.ToArray(); // Note: When presented with a constructor that accepts a variable number of // arguments, ReSharper treats them as a sequence of normal parameter // values. So we we need to map them back into a params array appropriately. ConstantValue[] args = new ConstantValue[parameters.Count]; values.CopyTo(0, args, 0, lastParameterIndex); int varArgsCount = values.Count - lastParameterIndex; ConstantValue[] varArgs = new ConstantValue[varArgsCount]; for (int i = 0; i < varArgsCount; i++) varArgs[i] = values[lastParameterIndex + i]; args[lastParameterIndex] = new ConstantValue(MakeType(lastParameter.Type), varArgs); return args; }
private ConstantValue ConvertConstantValue(CustomAttributeArgument constant) { var type = MakeType(constant.Type); if (constant.Value != null) { var typeRef = constant.Value as TypeReference; if (typeRef != null) return new ConstantValue(type, MakeType(typeRef)); var arrayType = constant.Type as ArrayType; if (arrayType != null) { var arrayConstants = (CustomAttributeArgument[])constant.Value; var length = arrayConstants.Length; var array = new ConstantValue[length]; for (var i = 0; i < length; i++) array[i] = ConvertConstantValue(arrayConstants[i]); return new ConstantValue(type, array); } } return new ConstantValue(type, constant.Value); }