|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionAt my current employer, the need for being able to "Sign" some additional information to an assembly came about. As an option, I could have always just created a I happened to come across a project that had very little documentation called Basically when it comes down to it, in an effort to find a solution to a simple problem, I came across a tool that was way more powerful than I had ever heard of. I intend to start out with some very basic examples and eventually dive into some more complex examples, so stay tuned! The Custom "Signing" ExampleFor this example, I'll be signing a simple console application with some I'm no expert with the post compilation tasks, but basically you'll want to compile using System;
using System.Collections.Generic;
using System.Text;
using Mono.Cecil;
namespace AppSigner
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Signing DummyApp.exe");
// create an Assembly definition from target assembly...
// in this case I'll pass in full path to DummyApp.exe
AssemblyDefinition sourceAssembly
= AssemblyFactory.GetAssembly("DummyApp.exe");
//Next, we'll need to "Import" a reference of the new
//CustomAttribute (which happens to exist in my signing
//app for the moment.)
//typeof(string) denotes what the constructor parameter
//type is. If your attribute was of Guid you could use
//typeof(Guid) etc.
CustomAttribute ca =
new CustomAttribute(sourceAssembly.MainModule.Import(
typeof(AssemblyExtendedInfo).GetConstructor(
new Type[] { typeof(string) })));
//Assign the parameters value of specified type
ca.ConstructorParameters.Add("This is some extended information!");
//go ahead and now add this customattribute instance
// back to the target assembly's CustomAttributes collection.
sourceAssembly.CustomAttributes.Add(ca);
//finally go ahead and persist back to disk. In this case
//for clarity, I went ahead and just wrote a new file out.
AssemblyFactory.SaveAssembly(sourceAssembly, "newassembly.exe");
Console.WriteLine("Signing Complete!");
Console.WriteLine("Verifying...");
//now just to verify, we'll open the new assembly back up,
//and loop through custom attributes...
AssemblyDefinition targetAssembly
= AssemblyFactory.GetAssembly("newassembly.exe");
foreach (CustomAttribute eca in targetAssembly.CustomAttributes)
{
if (eca.Constructor.DeclaringType.Name ==
"AssemblyExtendedInfo")
{
Console.WriteLine(
"newassembly.exe's ApplicationExtendedInformation attribute:");
Console.WriteLine(eca.ConstructorParameters[0].ToString());
}
}
Console.ReadLine();
}
}
}
You also need to define the using System;
using System.Collections.Generic;
using System.Text;
namespace AppSigner
{
//mark this attribute as applicable to Assemblies
[AttributeUsage(AttributeTargets.Assembly)]
public sealed class AssemblyExtendedInfo : Attribute
{
private string extendedInfo = string.Empty;
public string ExtenededInfo
{
get
{
return extendedInfo;
}
}
//this will be the constructor we'll be referring to later
public AssemblyExtendedInfo(string extendedInfo)
{
this.extendedInfo = extendedInfo;
}
public override string ToString()
{
return extendedInfo;
}
}
}
Points of InterestNow many of you will wonder what the point is to this exercise considering that the new What's To Come?I intend to follow up with at least two further articles explaining the capabilities of Article History
|
||||||||||||||||||||||