Hi,
I want to add custom attributes to all the methods of a class that takes
"Type" as argument
For example:
*Assembly1 (TestApplication) : has all the methods*
class SampleItems
{
public static void Main()
{
SampleItems f= new SampleItems ();
f.Sample ();
Console .ReadLine ();
}
public void Sample()
{
Console.WriteLine("sample");
}
public void Sample1()
{
Console .WriteLine ("sample1");
}
}
Attribute is defined in the *assembly2* (*(Sample.dll)*) and also it has
the class to be applied for annotation
[System.AttributeUsage(AttributeTargets.Class | AttributeTargets.Method,
AllowMultiple=true )]
public sealed class SampleClass :Attribute
{
public Annotation InputType
{
get;
set;
}
public SampleClass (Annotation Test)
{
if(Test is Annotation )
{
InputType =Test ;
}
}
}
public class Annotation
{
public void TestingTers(){
Console .WriteLine ("This is sample for testing ters");
}
}
*And I am mixing the attribute via the following code in a separate exe*
var sampleDll = AssemblyDefinition.ReadAssembly("SampleDll.dll");
var targetExe =
AssemblyDefinition.ReadAssembly(@"TestApplication.exe");
foreach (var typeDef in sampleDll.Modules)
{
foreach (var typeDefname in typeDef.Types)
{
if (typeDefname .Name =="SampleClass")
{
var sampleClass = sampleDll.MainModule.GetType("SampleDll." +
typeDefname.Name);
var sampleClassCtor = sampleClass.Methods.First(m =>
m.IsConstructor);
var ctorReference = targetExe.MainModule.Import(sampleClassCtor);
foreach (var type in targetExe.MainModule.Types)
{
foreach (var methodDef in type.Methods)
{
var custatt = new CustomAttribute(ctorReference);
custatt.ConstructorArguments.Add(new CustomAttributeArgument(
targetExe .MainModule .TypeSystem.String ,
sampleDll.MainModule.GetType("SampleDll.Ters").FullName ));
*//How to add constructor argument that take a type as input*
*//Please suggest me*
methodDef.CustomAttributes.Add(custatt);
}
}
}
}
}
targetExe.Write(@"TestApplicationupdate.exe");
Console .ReadLine ();
}
So the Final output will be
=====================
*[SampleClass(Annotation)]*
* public void Sample1()*
* {*
* Console .WriteLine ("sample1");*
* }*
*looking forward for your reply*
--
--
mono-cecil