public ProxyGeneratorResult GenerateProxy( Type typeOfProxy, IEnumerable <Type> additionalInterfacesToImplement, IEnumerable <object> argumentsForConstructor, IEnumerable <Expression <Func <Attribute> > > attributes, IFakeCallProcessorProvider fakeCallProcessorProvider) { Guard.AgainstNull(typeOfProxy, nameof(typeOfProxy)); Guard.AgainstNull(additionalInterfacesToImplement, nameof(additionalInterfacesToImplement)); Guard.AgainstNull(attributes, nameof(attributes)); Guard.AgainstNull(fakeCallProcessorProvider, nameof(fakeCallProcessorProvider)); if (!this.CanGenerateProxy(typeOfProxy, out string failReason)) { return(new ProxyGeneratorResult(failReason)); } GuardAgainstConstructorArgumentsForInterfaceType(typeOfProxy, argumentsForConstructor); var options = CreateProxyGenerationOptions(); foreach (var attribute in attributes) { options.AdditionalAttributes.Add(CustomAttributeInfo.FromExpression(attribute)); } return(CreateProxyGeneratorResult(typeOfProxy, options, additionalInterfacesToImplement, argumentsForConstructor, fakeCallProcessorProvider)); }
private static object[] CreateFromExpressionTestCase( Expression <Func <Attribute> > expr, CustomAttributeInfo expected ) { return(new object[] { new Opaque <Expression <Func <Attribute> > >(expr), expected }); }
public void Equals_DifferentAdditionalAttributesDuplicateEntries() { var info11 = new CustomAttributeInfo( typeof(DescriptionAttribute).GetConstructor(new[] { typeof(string) }), new object[] { "Test1" } ); var info12 = new CustomAttributeInfo( typeof(DescriptionAttribute).GetConstructor(new[] { typeof(string) }), new object[] { "Test1" } ); var info13 = new CustomAttributeInfo( typeof(DescriptionAttribute).GetConstructor(new[] { typeof(string) }), new object[] { "Test1" } ); var info2 = new CustomAttributeInfo( typeof(DescriptionAttribute).GetConstructor(new[] { typeof(string) }), new object[] { "Test2" } ); _options1.AdditionalAttributes.Add(info11); _options1.AdditionalAttributes.Add(info12); _options2.AdditionalAttributes.Add(info13); _options2.AdditionalAttributes.Add(info2); Assert.AreNotEqual(_options1, _options2); }
public void GetHashCode_DifferentOptions_SameAdditionalAttributesButDuplicateEntries() { var info1 = new CustomAttributeInfo( typeof(DescriptionAttribute).GetConstructor(new[] { typeof(string) }), new object[] { "Test1" } ); var info2 = new CustomAttributeInfo( typeof(DescriptionAttribute).GetConstructor(new[] { typeof(string) }), new object[] { "Test1" } ); var info3 = new CustomAttributeInfo( typeof(DescriptionAttribute).GetConstructor(new[] { typeof(string) }), new object[] { "Test1" } ); var info4 = new CustomAttributeInfo( typeof(DescriptionAttribute).GetConstructor(new[] { typeof(string) }), new object[] { "Test1" } ); _options1.AdditionalAttributes.Add(info1); _options2.AdditionalAttributes.Add(info2); _options2.AdditionalAttributes.Add(info3); _options2.AdditionalAttributes.Add(info4); Assert.AreNotEqual(_options1.GetHashCode(), _options2.GetHashCode()); }
public void GetHashCode_EqualOptions_SameAdditionalAttributesDifferentOrder() { var info11 = new CustomAttributeInfo( typeof(DescriptionAttribute).GetConstructor(new[] { typeof(string) }), new object[] { "Test1" } ); var info12 = new CustomAttributeInfo( typeof(DescriptionAttribute).GetConstructor(new[] { typeof(string) }), new object[] { "Test1" } ); var info21 = new CustomAttributeInfo( typeof(DescriptionAttribute).GetConstructor(new[] { typeof(string) }), new object[] { "Test2" } ); var info22 = new CustomAttributeInfo( typeof(DescriptionAttribute).GetConstructor(new[] { typeof(string) }), new object[] { "Test2" } ); _options1.AdditionalAttributes.Add(info11); _options1.AdditionalAttributes.Add(info21); _options2.AdditionalAttributes.Add(info22); _options2.AdditionalAttributes.Add(info12); Assert.AreEqual(_options1.GetHashCode(), _options2.GetHashCode()); }
public void FromExpression_Creates_Same_CustomAttributeInfo_As_Calling_The_Constructor( Opaque <Expression <Func <Attribute> > > expr, CustomAttributeInfo expected) { var actual = CustomAttributeInfo.FromExpression(expr.Value); Assert.AreEqual(expected, actual); }
private Attribute[] GetPropertyAttributes(CustomAttributeInfo attrInfo) { DefaultValueAttribute defaultValueAttribute = null; if (attrInfo.DefaultValue.GetType() == attrInfo.ValueType) { defaultValueAttribute = new DefaultValueAttribute(attrInfo.DefaultValue); goto IL_0054; } if (attrInfo.DefaultValue is string) { defaultValueAttribute = new DefaultValueAttribute(attrInfo.ValueType, (string)attrInfo.DefaultValue); goto IL_0054; } throw new InvalidOperationException(SR.ExceptionCustomAttributeDefaultValueTypeInvalid); IL_0054: ArrayList arrayList = new ArrayList(); arrayList.Add(new NotifyParentPropertyAttribute(true)); arrayList.Add(new RefreshPropertiesAttribute(RefreshProperties.All)); arrayList.Add(new DescriptionAttribute(attrInfo.Description)); arrayList.Add(defaultValueAttribute); int num = 0; Attribute[] array = new Attribute[arrayList.Count]; foreach (Attribute item in arrayList) { array[num++] = item; } return(array); }
private Attribute[] GetPropertyAttributes(CustomAttributeInfo attrInfo) { DefaultValueAttribute defaultValueAttribute = null; if (attrInfo.DefaultValue.GetType() == attrInfo.ValueType) { defaultValueAttribute = new DefaultValueAttribute(attrInfo.DefaultValue); } else { if (!(attrInfo.DefaultValue is string)) { throw new InvalidOperationException(SR.ExceptionCustomAttributeDefaultValueTypeInvalid); } defaultValueAttribute = new DefaultValueAttribute(attrInfo.ValueType, (string)attrInfo.DefaultValue); } ArrayList obj = new ArrayList { new NotifyParentPropertyAttribute(notifyParent: true), new RefreshPropertiesAttribute(RefreshProperties.All), new DescriptionAttribute(attrInfo.Description), defaultValueAttribute }; int num = 0; Attribute[] array = new Attribute[obj.Count]; foreach (Attribute item in obj) { array[num++] = item; } return(array); }
public void Attributes_Of_Same_Type_With_Different_Constructor_Array_Arguments_Are_Not_Equal() { var x = CustomAttributeInfo.FromExpression(() => new MyAttribute1(42, "foo", new[] { 1, 2, 3 })); var y = CustomAttributeInfo.FromExpression(() => new MyAttribute1(99, "foo", new[] { 1, 2, 4 })); Assert.AreNotEqual(x, y); }
public void Attributes_Of_Same_Type_With_Same_Constructor_Arguments_Are_Equal() { var x = CustomAttributeInfo.FromExpression(() => new MyAttribute1(42, "foo", new[] { 1, 2, 3 })); var y = CustomAttributeInfo.FromExpression(() => new MyAttribute1(42, "foo", new[] { 1, 2, 3 })); Assert.AreEqual(x, y); Assert.AreEqual(x.GetHashCode(), y.GetHashCode()); }
public static ProxyGeneratorResult GenerateClassProxy( Type typeOfProxy, ReadOnlyCollection <Type> additionalInterfacesToImplement, IEnumerable <object> argumentsForConstructor, IEnumerable <Expression <Func <Attribute> > > attributes, IFakeCallProcessorProvider fakeCallProcessorProvider) { Guard.AgainstNull(typeOfProxy, nameof(typeOfProxy)); Guard.AgainstNull(additionalInterfacesToImplement, nameof(additionalInterfacesToImplement)); Guard.AgainstNull(attributes, nameof(attributes)); Guard.AgainstNull(argumentsForConstructor, nameof(argumentsForConstructor)); Guard.AgainstNull(fakeCallProcessorProvider, nameof(fakeCallProcessorProvider)); if (!CanGenerateProxy(typeOfProxy, out string failReason)) { return(new ProxyGeneratorResult(failReason)); } var options = CreateProxyGenerationOptions(); foreach (var attribute in attributes) { options.AdditionalAttributes.Add(CustomAttributeInfo.FromExpression(attribute)); } Type[] allInterfacesToImplement; if (additionalInterfacesToImplement.Count == 0) { allInterfacesToImplement = Type.EmptyTypes; } else { allInterfacesToImplement = new Type[additionalInterfacesToImplement.Count]; additionalInterfacesToImplement.CopyTo(allInterfacesToImplement, 0); } var argumentsArray = argumentsForConstructor.ToArray(); object proxy; try { proxy = ProxyGenerator.CreateClassProxy( typeOfProxy, allInterfacesToImplement, options, argumentsArray, new ProxyInterceptor(fakeCallProcessorProvider)); } catch (Exception e) { return(GetResultForFailedProxyGeneration(typeOfProxy, argumentsArray, e)); } fakeCallProcessorProvider.EnsureInitialized(proxy); return(new ProxyGeneratorResult(generatedProxy: proxy)); }
public void Equals_SameAdditionalAttributes() { var info1 = new CustomAttributeInfo(typeof(DescriptionAttribute).GetConstructor(new[] { typeof(string) }), new object[] { "Test" }); var info2 = new CustomAttributeInfo(typeof(DescriptionAttribute).GetConstructor(new[] { typeof(string) }), new object[] { "Test" }); _options1.AdditionalAttributes.Add(info1); _options2.AdditionalAttributes.Add(info2); Assert.AreEqual(_options1, _options2); }
private static object GeneratoProxy(Type type, string host, IInterceptor interceptor) { var option = new ProxyGenerationOptions(); if (string.IsNullOrEmpty(host) == false) { var ctor = typeof(HttpHostAttribute).GetConstructors().FirstOrDefault(); var hostAttribute = new CustomAttributeInfo(ctor, new object[] { host }); option.AdditionalAttributes.Add(hostAttribute); } return(generator.CreateInterfaceProxyWithoutTarget(type, option, interceptor)); }
/// <summary> /// 获取请求接口的实现对象 /// </summary> /// <typeparam name="TInterface">请求接口</typeparam> /// <param name="host">服务跟路径</param> /// <param name="interceptor">拦截器</param> /// <returns></returns> private static TInterface GeneratoProxy <TInterface>(string host, IInterceptor interceptor) where TInterface : class { var option = new ProxyGenerationOptions(); if (string.IsNullOrEmpty(host) == false) { var ctor = typeof(HttpHostAttribute).GetConstructors().FirstOrDefault(); var hostAttribute = new CustomAttributeInfo(ctor, new object[] { host }); option.AdditionalAttributes.Add(hostAttribute); } return(HttpApiClient.generator.CreateInterfaceProxyWithoutTarget <TInterface>(option, interceptor)); }
/// <summary> /// 获取请求接口的实现对象 /// </summary> /// <typeparam name="TInterface">请求接口</typeparam> /// <param name="host">服务跟路径,效果与HttpHostAttribute一致</param> /// <returns></returns> public TInterface GetHttpApi <TInterface>(string host) where TInterface : class { if (string.IsNullOrEmpty(host)) { throw new ArgumentNullException(); } var option = new ProxyGenerationOptions(); var ctor = typeof(HttpHostAttribute).GetConstructors().FirstOrDefault(); var hostAttribute = new CustomAttributeInfo(ctor, new object[] { host }); option.AdditionalAttributes.Add(hostAttribute); return(this.generator.CreateInterfaceProxyWithoutTarget <TInterface>(option, this)); }
public void Attributes_Of_Same_Type_With_Different_Array_Properties_Are_Not_Equal() { var x = CustomAttributeInfo.FromExpression( () => new MyAttribute1 { IntProperty = 42, StringProperty = "foo", ArrayProperty = new[] { 1, 2, 3 } }); var y = CustomAttributeInfo.FromExpression( () => new MyAttribute1 { IntProperty = 99, StringProperty = "foo", ArrayProperty = new[] { 1, 2, 4 } }); Assert.AreNotEqual(x, y); }
public void Attributes_Of_Same_Type_With_Different_Array_Fields_Are_Not_Equal() { var x = CustomAttributeInfo.FromExpression( () => new MyAttribute1 { intField = 42, stringField = "foo", arrayField = new[] { 1, 2, 3 } }); var y = CustomAttributeInfo.FromExpression( () => new MyAttribute1 { intField = 99, stringField = "foo", arrayField = new[] { 1, 2, 4 } }); Assert.AreNotEqual(x, y); }
public void Attributes_Of_Same_Type_With_Same_Fields_Are_Equal() { var x = CustomAttributeInfo.FromExpression( () => new MyAttribute1 { intField = 42, stringField = "foo", arrayField = new[] { 1, 2, 3 } }); var y = CustomAttributeInfo.FromExpression( () => new MyAttribute1 { intField = 42, stringField = "foo", arrayField = new[] { 1, 2, 3 } }); Assert.AreEqual(x, y); Assert.AreEqual(x.GetHashCode(), y.GetHashCode()); }
public void Attributes_Of_Same_Type_With_Same_Properties_Are_Equal() { var x = CustomAttributeInfo.FromExpression( () => new MyAttribute1 { IntProperty = 42, StringProperty = "foo", ArrayProperty = new[] { 1, 2, 3 } }); var y = CustomAttributeInfo.FromExpression( () => new MyAttribute1 { IntProperty = 42, StringProperty = "foo", ArrayProperty = new[] { 1, 2, 3 } }); Assert.AreEqual(x, y); Assert.AreEqual(x.GetHashCode(), y.GetHashCode()); }
//http://patriksvensson.se/2013/08/how-to-dynamically-add-attributes-to-a-class-with-castle-core/ public static Type CreateTypeDecoratedWithAttribute(Type classType) { if (classType == null || !classType.IsClass || classType.IsSealed) { throw new InvalidOperationException("Invalid type"); } lock (MetaTypeCache) { Type typeProxified = null; if (!MetaTypeCache.TryGetValue(classType, out typeProxified)) { // Create the proxy generation options. // This is how we tell Castle.DynamicProxy how to create the attribute. var proxyOptions = new ProxyGenerationOptions(); var attrTypes = new [] { typeof(ProtoBuf.ProtoContractAttribute), typeof(DataContractAttribute) }; foreach (var attrType in attrTypes) { if (!classType.CustomAttributes.Any(e => e.AttributeType == attrType)) { // Get the attribute constructor. Type[] ctorTypes = Type.EmptyTypes; var ctor = attrType.GetConstructor(ctorTypes); Debug.Assert(ctor != null, "Could not get constructor for attribute."); // Create an attribute builder. object[] attributeArguments = new object[] { }; //var builder = new CustomAttributeBuilder(ctor, attributeArguments); var builder = new CustomAttributeInfo(ctor, attributeArguments); proxyOptions.AdditionalAttributes.Add(builder); } } // Create the proxy generator. var proxyGenerator = new ProxyGenerator(); //proxyGenerator.ProxyBuilder.CreateClassProxyType() var newGenerator = new ProxyGeneratorAttributePropertyTest(proxyGenerator.ProxyBuilder.ModuleScope, classType) { Logger = proxyGenerator.ProxyBuilder.Logger }; MetaTypeCache.Add(classType, typeProxified = newGenerator.GenerateCode(Type.EmptyTypes, proxyOptions)); } return(typeProxified); } }
public ProxyGeneratorResult GenerateProxy( Type typeOfProxy, IEnumerable <Type> additionalInterfacesToImplement, IEnumerable <object> argumentsForConstructor, IEnumerable <Expression <Func <Attribute> > > attributes, IFakeCallProcessorProvider fakeCallProcessorProvider) { Guard.AgainstNull(attributes, nameof(attributes)); var options = CreateProxyGenerationOptions(); foreach (var attribute in attributes) { options.AdditionalAttributes.Add(CustomAttributeInfo.FromExpression(attribute)); } return(GenerateProxy(typeOfProxy, options, additionalInterfacesToImplement, argumentsForConstructor, fakeCallProcessorProvider)); }
private bool IsApplicableCustomAttribute(CustomAttributeInfo attrInfo, object obj) { if (obj is CustomAttributes) { obj = ((CustomAttributes)obj).DataPointAttributes; } if (this.IsDataPoint(obj) && attrInfo.AppliesToDataPoint) { goto IL_0037; } if (!this.IsDataPoint(obj) && attrInfo.AppliesToSeries) { goto IL_0037; } goto IL_00a6; IL_00a6: return(false); IL_0037: if (this.Is3DChartType(obj) && attrInfo.AppliesTo3D) { goto IL_0059; } if (!this.Is3DChartType(obj) && attrInfo.AppliesTo2D) { goto IL_0059; } goto IL_00a6; IL_0059: SeriesChartType[] selectedChartTypes = this.GetSelectedChartTypes(obj); SeriesChartType[] array = selectedChartTypes; foreach (SeriesChartType seriesChartType in array) { SeriesChartType[] appliesToChartType = attrInfo.AppliesToChartType; foreach (SeriesChartType seriesChartType2 in appliesToChartType) { if (seriesChartType2 == seriesChartType) { return(true); } } } goto IL_00a6; }
public static ProxyGeneratorResult GenerateInterfaceProxy( Type typeOfProxy, ReadOnlyCollection <Type> additionalInterfacesToImplement, IEnumerable <Expression <Func <Attribute> > > attributes, IFakeCallProcessorProvider fakeCallProcessorProvider) { Guard.AgainstNull(typeOfProxy, nameof(typeOfProxy)); Guard.AgainstNull(additionalInterfacesToImplement, nameof(additionalInterfacesToImplement)); Guard.AgainstNull(attributes, nameof(attributes)); Guard.AgainstNull(fakeCallProcessorProvider, nameof(fakeCallProcessorProvider)); var options = CreateProxyGenerationOptions(); foreach (var attribute in attributes) { options.AdditionalAttributes.Add(CustomAttributeInfo.FromExpression(attribute)); } var allInterfacesToImplement = new Type[1 + additionalInterfacesToImplement.Count]; additionalInterfacesToImplement.CopyTo(allInterfacesToImplement, 1); allInterfacesToImplement[0] = typeOfProxy; object proxy; try { proxy = ProxyGenerator.CreateClassProxy( typeof(object), allInterfacesToImplement, options, (object[])null, new ProxyInterceptor(fakeCallProcessorProvider)); } catch (Exception e) { return(GetResultForFailedProxyGeneration(typeOfProxy, null, e)); } fakeCallProcessorProvider.EnsureInitialized(proxy); return(new ProxyGeneratorResult(generatedProxy: proxy)); }
private bool IsApplicableCustomAttribute(CustomAttributeInfo attrInfo, object obj) { if (obj is CustomAttributes) { obj = ((CustomAttributes)obj).DataPointAttributes; } if (((IsDataPoint(obj) && attrInfo.AppliesToDataPoint) || (!IsDataPoint(obj) && attrInfo.AppliesToSeries)) && ((Is3DChartType(obj) && attrInfo.AppliesTo3D) || (!Is3DChartType(obj) && attrInfo.AppliesTo2D))) { SeriesChartType[] selectedChartTypes = GetSelectedChartTypes(obj); foreach (SeriesChartType seriesChartType in selectedChartTypes) { SeriesChartType[] appliesToChartType = attrInfo.AppliesToChartType; for (int j = 0; j < appliesToChartType.Length; j++) { if (appliesToChartType[j] == seriesChartType) { return(true); } } } } return(false); }
internal static CustomAttributeInfo decode_cattr (CustomAttributeBuilder customBuilder) { byte[] data = customBuilder.Data; ConstructorInfo ctor = customBuilder.Ctor; int pos = 0; CustomAttributeInfo info = new CustomAttributeInfo (); // Prolog if (data.Length < 2) throw new Exception ("Custom attr length is only '" + data.Length + "'"); if ((data [0] != 0x1) || (data [1] != 0x00)) throw new Exception ("Prolog invalid"); pos = 2; ParameterInfo [] pi = GetParameters (ctor); info.ctor = ctor; info.ctorArgs = new object [pi.Length]; for (int i = 0; i < pi.Length; ++i) info.ctorArgs [i] = decode_cattr_value (pi [i].ParameterType, data, pos, out pos); int num_named = data [pos] + (data [pos + 1] * 256); pos += 2; info.namedParamNames = new string [num_named]; info.namedParamValues = new object [num_named]; for (int i = 0; i < num_named; ++i) { int named_type = data [pos++]; int data_type = data [pos++]; string enum_type_name = null; if (data_type == 0x55) { int len2 = decode_len (data, pos, out pos); enum_type_name = string_from_bytes (data, pos, len2); pos += len2; } int len = decode_len (data, pos, out pos); string name = string_from_bytes (data, pos, len); info.namedParamNames [i] = name; pos += len; if (named_type == 0x53) { /* Field */ FieldInfo fi = ctor.DeclaringType.GetField (name, BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance); if (fi == null) throw new Exception ("Custom attribute type '" + ctor.DeclaringType + "' doesn't contain a field named '" + name + "'"); object val = decode_cattr_value (fi.FieldType, data, pos, out pos); if (enum_type_name != null) { Type enumType = Type.GetType (enum_type_name); val = Enum.ToObject (enumType, val); } info.namedParamValues [i] = val; } else // FIXME: throw new Exception ("Unknown named type: " + named_type); } return info; }
public void Equals_DifferentAdditionalAttributesDuplicateEntries() { var info11 = new CustomAttributeInfo(typeof(DescriptionAttribute).GetConstructor(new[] { typeof(string) }), new object[] { "Test1" }); var info12 = new CustomAttributeInfo(typeof(DescriptionAttribute).GetConstructor(new[] { typeof(string) }), new object[] { "Test1" }); var info13 = new CustomAttributeInfo(typeof(DescriptionAttribute).GetConstructor(new[] { typeof(string) }), new object[] { "Test1" }); var info2 = new CustomAttributeInfo(typeof(DescriptionAttribute).GetConstructor(new[] { typeof(string) }), new object[] { "Test2" }); _options1.AdditionalAttributes.Add(info11); _options1.AdditionalAttributes.Add(info12); _options2.AdditionalAttributes.Add(info13); _options2.AdditionalAttributes.Add(info2); Assert.AreNotEqual(_options1, _options2); }
public void GetHashCode_EqualOptions_SameAdditionalAttributesDifferentOrder() { var info11 = new CustomAttributeInfo(typeof(DescriptionAttribute).GetConstructor(new[] { typeof(string) }), new object[] { "Test1" }); var info12 = new CustomAttributeInfo(typeof(DescriptionAttribute).GetConstructor(new[] { typeof(string) }), new object[] { "Test1" }); var info21 = new CustomAttributeInfo(typeof(DescriptionAttribute).GetConstructor(new[] { typeof(string) }), new object[] { "Test2" }); var info22 = new CustomAttributeInfo(typeof(DescriptionAttribute).GetConstructor(new[] { typeof(string) }), new object[] { "Test2" }); _options1.AdditionalAttributes.Add(info11); _options1.AdditionalAttributes.Add(info21); _options2.AdditionalAttributes.Add(info22); _options2.AdditionalAttributes.Add(info12); Assert.AreEqual(_options1.GetHashCode(), _options2.GetHashCode()); }
public void GetHashCode_DifferentOptions_SameAdditionalAttributesButDuplicateEntries() { var info1 = new CustomAttributeInfo(typeof(DescriptionAttribute).GetConstructor(new[] { typeof(string) }), new object[] { "Test1" }); var info2 = new CustomAttributeInfo(typeof(DescriptionAttribute).GetConstructor(new[] { typeof(string) }), new object[] { "Test1" }); var info3 = new CustomAttributeInfo(typeof(DescriptionAttribute).GetConstructor(new[] { typeof(string) }), new object[] { "Test1" }); var info4 = new CustomAttributeInfo(typeof(DescriptionAttribute).GetConstructor(new[] { typeof(string) }), new object[] { "Test1" }); _options1.AdditionalAttributes.Add(info1); _options2.AdditionalAttributes.Add(info2); _options2.AdditionalAttributes.Add(info3); _options2.AdditionalAttributes.Add(info4); Assert.AreNotEqual(_options1.GetHashCode(), _options2.GetHashCode()); }
internal CustomAttributesPropertyDescriptor(Type componentType, string name, Type propertyType, Attribute[] attributes, CustomAttributeInfo customAttributeInfo) : base(componentType, name, propertyType, attributes) { this.name = name; this.customAttributeInfo = customAttributeInfo; }
internal static CustomAttributeInfo decode_cattr(CustomAttributeBuilder customBuilder) { byte[] data = customBuilder.Data; ConstructorInfo ctor = customBuilder.Ctor; int pos = 0; CustomAttributeInfo info = new CustomAttributeInfo(); // Prolog if (data.Length < 2) { throw new Exception("Custom attr length is only '" + data.Length + "'"); } if ((data [0] != 0x1) || (data [1] != 0x00)) { throw new Exception("Prolog invalid"); } pos = 2; ParameterInfo [] pi = GetParameters(ctor); info.ctor = ctor; info.ctorArgs = new object [pi.Length]; for (int i = 0; i < pi.Length; ++i) { info.ctorArgs [i] = decode_cattr_value(pi [i].ParameterType, data, pos, out pos); } int num_named = data [pos] + (data [pos + 1] * 256); pos += 2; info.namedParamNames = new string [num_named]; info.namedParamValues = new object [num_named]; for (int i = 0; i < num_named; ++i) { int named_type = data [pos++]; int data_type = data [pos++]; string enum_type_name = null; if (data_type == 0x55) { int len2 = decode_len(data, pos, out pos); enum_type_name = string_from_bytes(data, pos, len2); pos += len2; } int len = decode_len(data, pos, out pos); string name = string_from_bytes(data, pos, len); info.namedParamNames [i] = name; pos += len; if (named_type == 0x53) { /* Field */ FieldInfo fi = ctor.DeclaringType.GetField(name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); if (fi == null) { throw new Exception("Custom attribute type '" + ctor.DeclaringType + "' doesn't contain a field named '" + name + "'"); } object val = decode_cattr_value(fi.FieldType, data, pos, out pos); if (enum_type_name != null) { Type enumType = Type.GetType(enum_type_name); val = Enum.ToObject(enumType, val); } info.namedParamValues [i] = val; } else { // FIXME: throw new Exception("Unknown named type: " + named_type); } } return(info); }
public void FromExpression_Creates_Same_CustomAttributeInfo_As_Calling_The_Constructor( Expression<Func<Attribute>> expr, CustomAttributeInfo expected) { var actual = CustomAttributeInfo.FromExpression(expr); Assert.AreEqual(expected, actual); }
private static object[] CreateFromExpressionTestCase(Expression<Func<Attribute>> expr, CustomAttributeInfo expected) { return new object[] { expr, expected }; }