public CustomAttributeOptions(CustomAttribute ca)
 {
     this.RawData = ca.RawData;
     this.Constructor = ca.Constructor;
     this.ConstructorArguments.AddRange(ca.ConstructorArguments.Select(a => a.Clone()));
     this.NamedArguments.AddRange(ca.NamedArguments.Select(a => a.Clone()));
 }
		/// <summary>
		/// Inject the DevirtualizedAttribute into a method.
		/// </summary>
		/// <param name="method">Method to inject</param>
		public void InjectDevirtualized(MethodDef method)
		{
			if (!_initialized)
				this.Initialize();

			var customAttr = new CustomAttribute(_devirtualizedAttribute.FindMethod(".ctor"));
			method.CustomAttributes.Add(customAttr);
		}
			protected override void Execute(ConfuserContext context, ProtectionParameters parameters) {
				foreach (ModuleDef module in parameters.Targets.OfType<ModuleDef>()) {
					TypeRef attrRef = module.CorLibTypes.GetTypeRef("System.Runtime.CompilerServices", "SuppressIldasmAttribute");
					var ctorRef = new MemberRefUser(module, ".ctor", MethodSig.CreateInstance(module.CorLibTypes.Void), attrRef);

					var attr = new CustomAttribute(ctorRef);
					module.CustomAttributes.Add(attr);
				}
			}
Example #4
0
 public static string GetDefaultMemberName(this TypeDef type, out CustomAttribute defaultMemberAttribute)
 {
     if (type != null && type.HasCustomAttributes)
         foreach (CustomAttribute ca in type.CustomAttributes.FindAll("System.Reflection.DefaultMemberAttribute"))
             if (ca.Constructor != null && ca.Constructor.FullName == @"System.Void System.Reflection.DefaultMemberAttribute::.ctor(System.String)" &&
                 ca.ConstructorArguments.Count == 1 &&
                 ca.ConstructorArguments[0].Value is UTF8String) {
                 defaultMemberAttribute = ca;
                 return (UTF8String)ca.ConstructorArguments[0].Value;
             }
     defaultMemberAttribute = null;
     return null;
 }
Example #5
0
		public static string GetCustomArgAsString(CustomAttribute cattr, int arg) {
			if (cattr == null || arg >= cattr.ConstructorArguments.Count)
				return null;
			var carg = cattr.ConstructorArguments[arg];
			if (carg.Type.GetElementType() != ElementType.String)
				return null;
			return UTF8String.ToSystemStringOrEmpty((UTF8String)carg.Value);
		}
Example #6
0
		static bool GetMethodImplOptions(CustomAttribute cattr, ref int value) {
			if (cattr.IsRawBlob)
				return false;
			if (cattr.ConstructorArguments.Count != 1)
				return false;
			if (cattr.ConstructorArguments[0].Type.ElementType != ElementType.I2 &&
				cattr.ConstructorArguments[0].Type.FullName != "System.Runtime.CompilerServices.MethodImplOptions")
				return false;

			var arg = cattr.ConstructorArguments[0].Value;
			if (arg is short) {
				value = (short)arg;
				return true;
			}
			if (arg is int) {
				value = (int)arg;
				return true;
			}

			return false;
		}
Example #7
0
 void Add(CustomAttribute ca)
 {
     if (ca == null || customAttributes.ContainsKey(ca))
         return;
     customAttributes[ca] = true;
     Push(ca.Constructor);
     Add(ca.ConstructorArguments);
     Add(ca.NamedArguments);
 }
Example #8
0
		static bool IsIndexer(this PropertyDef property, out CustomAttribute defaultMemberAttribute)
		{
			defaultMemberAttribute = null;
			if (property != null && property.PropertySig.GetParamCount() > 0) {
				var accessor = property.GetMethod ?? property.SetMethod;
				PropertyDef basePropDef = property;
				if (accessor != null && accessor.HasOverrides) {
					// if the property is explicitly implementing an interface, look up the property in the interface:
					MethodDef baseAccessor = accessor.Overrides.First().MethodDeclaration.Resolve();
					if (baseAccessor != null) {
						foreach (PropertyDef baseProp in baseAccessor.DeclaringType.Properties) {
							if (baseProp.GetMethod == baseAccessor || baseProp.SetMethod == baseAccessor) {
								basePropDef = baseProp;
								break;
							}
						}
					} else
						return false;
				}
				CustomAttribute attr;
				var defaultMemberName = basePropDef.DeclaringType.GetDefaultMemberName(out attr);
				if (defaultMemberName == basePropDef.Name) {
					defaultMemberAttribute = attr;
					return true;
				}
			}
			return false;
		}
		public static string GetDefaultMemberName(this TypeDef type, out CustomAttribute defaultMemberAttribute)
		{
			if (type.HasCustomAttributes)
				foreach (CustomAttribute ca in type.CustomAttributes)
					if (ca.Constructor.DeclaringType.Name == "DefaultMemberAttribute" && ca.Constructor.DeclaringType.Namespace == "System.Reflection"
						&& ca.Constructor.FullName == @"System.Void System.Reflection.DefaultMemberAttribute::.ctor(System.String)") {
						defaultMemberAttribute = ca;
						return ca.ConstructorArguments[0].Value as UTF8String;
					}
			defaultMemberAttribute = null;
			return null;
		}
 private void Add(CustomAttribute obj)
 {
     AddToStack(obj);
 }
Example #11
0
 void Add(CustomAttribute obj) => AddToStack(obj);
		/// <summary>
		/// Creates a custom ObfuscationAttribute that can be added to a method.
		/// </summary>
		/// <param name="module">Module</param>
		/// <param name="feature">Obfuscation feature name</param>
		/// <param name="exclude">true if exclude, false if include</param>
		/// <returns>CustomAttribute</returns>
		CustomAttribute CreateAttribute(ModuleDef module, String feature, Boolean exclude)
		{
			TypeSig stringSig = module.CorLibTypes.String;
			TypeSig booleanSig = module.CorLibTypes.Boolean;

			CANamedArgument[] args = new CANamedArgument[] {
				// Feature
				new CANamedArgument(
					false,
					stringSig,
					"Feature",
					new CAArgument(stringSig, feature)),

				// Exclude
				new CANamedArgument(
					false,
					booleanSig,
					"Exclude",
					new CAArgument(booleanSig, exclude))
			};

			TypeRef obfuscationRef = new TypeRefUser(
				module, "System.Reflection", "ObfuscationAttribute", module.CorLibTypes.AssemblyRef);

			MemberRef obfuscationCtor = new MemberRefUser(module, ".ctor",
						MethodSig.CreateInstance(module.CorLibTypes.Void),
						obfuscationRef);

			CustomAttribute attr = new CustomAttribute(
				obfuscationCtor,
				new CAArgument[0],
				args
			);

			return attr;
		}