Removing an attribute.

Topics: Metadata Model
Jan 21, 2010 at 6:54 PM
Edited Jan 21, 2010 at 8:07 PM

Never mind figured it out. Here's some example code.

 

class Program
    {
        static void Main(string[] args)
        {
            var host = new PeReader.DefaultHost();
            var module = host.LoadUnitFrom(@"aFile") as IModule;

            // module = Decompiler.GetCodeModelFromMetadataModel(host, (IModule)module, null);

            var mutator = new AttributeRemover(host);
            module = mutator.Visit(module);

            //WRite out the new file?
            Stream peStream = File.Create(module.Location + ".pe");

            PeWriter.WritePeToStream(module, host, peStream);

        }
    }

    public class AttributeRemover : MetadataMutator
    {
        PlatformType pt;
        public AttributeRemover(IMetadataHost host)
            : base(host)
        {
            pt = new PlatformType(host);
        }
        public override List<ICustomAttribute> Visit(List<ICustomAttribute> customAttributes)
        {
            for (int i = 0; i < customAttributes.Count; i++)
            {

                if (customAttributes[i].Type.ToString() == "aAttributeNamespaceString")
                {
                    customAttributes.RemoveAt(i);
                    break;
                }
            }
            return base.Visit(customAttributes);
        }
    }