putting attributes into existing assemblies



Hello Everybody,

Iam new to C#. Is there a way to put custom attributes into already existing assemblies The source code for that assembly is not available.

Can I use any tools or hooks or does Reflection.Emit support this


Thank You.


Answer this question

putting attributes into existing assemblies

  • Mike Piska

    Ildasm / ilasm is the easiest way to modify an assembly that you don't have the source for.  If you're looking to figure out what the hex representation of a custom attribute is, you should be able to make a simple C# file that contains the attribute.  Then compile it, and use ildasm to get the hex.  You can then copy that into the .il file you want to modify.

    -Shawn



  • nelusan

    Hi again Vijayaraghava,
    You know, your ideas are interesting, and I wish I could be more helpful.
    In my first post, I was relating the concepts that I had read from my reading the Adam Nathan book. This was a stricly COM and Net Interop set of issues that he addressed in his book, because the initial release of the .NET Framework was kind of confusing and cumbersome when dealing with the whole Interop process.
    What you are suggesting sounds feasible, but any advice I could give you would be pure speculation. I don't see why you couldn't re-write the entire source file once you've got it dissassembled. (But, I think, if I remember correctly, that the disassembled file is constructed from the Microsoft Intermediate Language and metadata).
    Also, from what I've read, defining your own custom attributes is fairly simple, because you would be deriving from the .NET base attribute class. For that you will have to read the documentation (which is what I would be doing anyway). I suspect that you are probably far more knowledgeable than I am, and you'll come up with a better solution than I could ever dream up.

  • boulderbum


       Hello,
     
              Thank you for the reply. I just had a look at both the tools you mentioned & tried to deassemble & assemble it back. Please tell me how to edit this file & put new custom attributes into it. We have to write some hexa-decimal digits into it isn't How can we know that

               Thank you.

  • AYHAN

      Hello,

              Guess I wasn't clear with what I wanted. Given an assembly and a design model (in the form of XMI), I wanted to relate the software elements in the assembly (classes,methods etc) to the model. So I wanted to insert attributes (custom) into the assembly relating them.That's why I wanted to put custom attributes into an existing assembly.

              You said that this was possible using IlDasm & IlAsm.At that stage I didn't understand what the dis-assembled file contained. Just now I read an article which talks about IL Assembly Programming. But sadly, that didn't talk about attributes. Had only loops,jumps,classes. Can you specify some resources where I can find how to create custom attributes & put them in the assembly

                Thank You.



  • pwu123


       Hi,

              I was told that there is something called "Byte Code Engineering Library" for manipulation of Java Byte Code. Similar to that there is a "Microsoft Byte Code Engineering Library" (MBEL) for .NET. It is in the link given below.

             http://www.cs.arizona.edu/mbel

              But the thing I was asking was in the "limitations" section of that library.It seems the internal structure of custom attributes in .NET is complex.

              Can you specify some resources where I can find the internal structures & all those stuff about attributes in .NET assembly.

              Thank You.

  • Brian R. Bondy

    Yes, this is possible. You can use IlDasm.exe to provide a text file of your assembly. You can then edit this file and use the utility IlAsm.exe to compile your altered assembly. This is often done in Interop. 
    This is the MSDN info on IlDasm.exe: 
    http://msdn.microsoft.com/library/default.asp url=/library/en-us/cptools/html/cpconmsildisassemblerildasmexe.asp
    By the way, there is extensive description of this process in: ".NET and COM:The Complete Guide to Interoperability", by Adam Nathan .

  • Ruskin Dantra

    Ha Ha,...
    On reading your post, I really didn't understand that gibberish part. But, I can see that you have a sense of humor.  
    The reasoning behind the adding of pseudo-custom attributes to an assembly is usually straightforward. Typically the alterations made are minor, or to enable a more compatible interaction with either a COM component or some other assembly (or to provide a facility that the Type Library Importer did not). 
    You don't provide enough information. Is your assembly referenced by other assemblies or unmamnaged code  This needs to be taken into account.

  • sybakc


        Hi,

              I have started reading the documentation that comes with installed .NET version.They contain a lot of information on the structure of assemblies and IL Assembly programming.Putting custom attributes into source code is simple (by deriving from System.Attribute) but putting them into existing assembly without making it unusable is difficult, I guess.
              This is more like a tool that Iam thinking so making another file just for knowing the hex digits of custom attributes isn't feasible.Copy,paste won't work.It can be done manually though.

               I came across a set of APIs called RAIL : Runtime Assembly Instrumentation Library. Check out the link.

                          http://rail.dei.uc.pt

               It is similar to MBEL but has more features & itseems it is a high-level language API. It is in C#. They have implemented custom attributes.What are your thoughts & comments on that.

               Thank you.

  • putting attributes into existing assemblies