Dummy-Lib
1.3.0
See the version list below for details.
dotnet add package Dummy-Lib --version 1.3.0
NuGet\Install-Package Dummy-Lib -Version 1.3.0
<PackageReference Include="Dummy-Lib" Version="1.3.0" />
paket add Dummy-Lib --version 1.3.0
#r "nuget: Dummy-Lib, 1.3.0"
// Install Dummy-Lib as a Cake Addin #addin nuget:?package=Dummy-Lib&version=1.3.0 // Install Dummy-Lib as a Cake Tool #tool nuget:?package=Dummy-Lib&version=1.3.0
Dummy-Lib
Opening an AssemblyWriter
To open an Assembly Writer it is pretty simple There are around 6 AssemblyWriter CTORs but I will just use the longest one so you can get an understanding
// Should the assemblywriter throw its internal errors? Should the Writer backup the targetfile?
//AssemblyWriter(string targetFile, string output, bool throwInternalErrors, bool backUpFile)
AssemblyWriter writer = new AssemblyWriter("Target.dll", "dllToWriteTo.dll", true, true);
Creating a Method
AssemblyWriter writer = new AssemblyWriter("target.dll");
//find a TargetType in the target.dll
TargetType typeTarg = new TargetType("Namespace.TypeName");
We also need a class that inherits MethodCreator so let's do that first
public class MethodToInsert : MethodCreator
{
//The interface requires you to add a Type[] property, this array should include the types of your methods arguments as follows
public Type[] MethodArgs { get { return new Type[] { typeof(int), typeof(int) }; } }
//This override is the name of your method, the method's name will remain as "Method" until overriden
public override string Name { get => "Add"; set => base.Name = value; }
//now the abstract class does not require you to have a method but the AssemblyWriter.CreateMethod does
//your methods name should be named after the overriden name you provide and that is about it for requirements
//(Note: any attribute you add to this method will be applied to the method created)
public static int Add(int a, int b)
{
return a + b;
}
}
Let's head back to our main code
AssemblyWriter writer = new AssemblyWriter("target.dll");
TargetType typeTarg = new TargetType("Namespace.TypeName");
//make a usermethod with a name and MethodCreator
UserMethod yourMethod = new UserMethod(new MethodToInsert(), "AddNums");
//let's add our method
CreatedMethod method = writer.CreateMethod(typeTarg, yourMethod);
//we can then save our method to the output by using the save method
writer.Save();
Creating A Field
Creating a field is pretty simple so let's do it
//open a writer
AssemblyWriter writer = new AssemblyWriter("target.dll");
//find a TargetType in the target.dll
TargetType typeTarg = new TargetType("Namespace.TypeName");
//make a field
UserField<string> uField = new UserField<string>("MyFieldName", "Value", FieldAttributes.Public);
//A Field Is generated inside of the typeTarg
FieldDef newField = writer.CreateField<string>(typeTarg, ufield);
//save to the target
writer.Save();
Creating A Property
Creating a property is a little more difficult so let me show you how to do it
//First make a class that inherits the Property<T>
public class MyProp : Property<string>
{
public MyProp(string name) : base(name) {}
//this will be based off of your T
public override string Val { get; set; }
//this will be called everytime your property is got
public override string GetMethod()
{
//code goes here
return "Val";
}
//this will be called everytime your property is set
public override void SetMethod()
{
//code goes here
}
/*
what if we dont want to return a value?
or we want our get and set methods to be empty?
just do this!
public override GetMethod()
{
throw NewNotImplementedException("EMPTY");
}
public override SetMethod()
{
throw NewNotImplementedException("EMPTY");
}
//generates public T NameHere { get; set; }
*/
}
//now let's write the property
//open the writer
AssemblyWriter writer = new AssemblyWriter("target.dll");
//define the Target type
TargetType typeTarg = new TargetType("Namespace.TypeName");
//define our method attributes
MethodAttributes methodAttrs = MethodAttributes.Public | MethodAttributes.Static;
//create the property
PropertyDef propDef = writer.CreateProperty<string>(typeTarg, new MyProp("PropName"), methodAttrs);
//write to the target
writer.Save();
Creating Instructions In A Method
I will now show you how to add instructions to a method
//Open the writer
AssemblyWriter writer = new AssemblyWriter("Target.dll");
//Find the type
TargetType typeTarg = new TargetType("Namespace.Type");
//Find the method with the name and the types that the arguments are
TargetMethod method = new TargetMethod("MethodName", new Type[] { });
//create a Instruction Point to be used in the AddInstruction method
InstructionPoint inPoint = InstructionCreater.CreateInstruction(OpCodes.Call, null, 10);
//remove the instruction from the method at index 10
writer.RemoveInstruction(typeTarg, method, 10);
//add the instruction to the method
writer.AddInstruction(typeTarg, method, inPoint);
//save to the output
writer.Save();
//the writer contains another method for adding and removing instructions,
//this allows you to add multiple instructions or even remove multiple instructions at the same time
Adding An Existing Type
This is even simpiler than creating your own Type
//open a writer
AssemblyWriter writer = new AssemblyWriter("Target.dll");
//Supports Inheritance, adds all types that are inherited from your class you want to add
/*
public class A : B, IMain {}
public class B : C {}
public class C {}
public interface IMain {}
*/
TypeDef tDef = writer.AddExistingType(typeof(A));
//Adds all following Types -> C -> B -> IMain -> A | In this order
//save to file
writer.Save();
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.1 is compatible. |
-
.NETCoreApp 3.1
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Allows properties to be added in Existing Types and other things added check GitHub Source