本文整理汇总了C#中ModuleDef.Import方法的典型用法代码示例。如果您正苦于以下问题:C# ModuleDef.Import方法的具体用法?C# ModuleDef.Import怎么用?C# ModuleDef.Import使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ModuleDef
的用法示例。
在下文中一共展示了ModuleDef.Import方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SetAssemblyAttribute
private static bool SetAssemblyAttribute(ModuleDef moduleDef, Type attributeType, string value)
{
var attributeAssemblyFileName = attributeType.Module.FullyQualifiedName;
using (var attributeModule = ModuleDefMD.Load(attributeAssemblyFileName))
{
var attributeTypeRef = moduleDef.Import(attributeType);
var attributeTypeDef = attributeModule.GetTypes().Single(t => t.FullName == attributeTypeRef.FullName);
var existingAttribute = moduleDef.Assembly.CustomAttributes.SingleOrDefault(t => t.TypeFullName == attributeTypeDef.FullName);
if (existingAttribute != null)
{
// if it exists and is already initialized with the same value, then no need to change it
if (((UTF8String)existingAttribute.ConstructorArguments[0].Value).String == value)
return false;
moduleDef.Assembly.CustomAttributes.Remove(existingAttribute);
}
var ctor = moduleDef.Import(attributeTypeDef.FindConstructors().Single());
var stringTypeSig = moduleDef.Import(typeof(string)).ToTypeSig();
moduleDef.Assembly.CustomAttributes.Add(new CustomAttribute(ctor, new[] { new CAArgument(stringTypeSig, new UTF8String(value)) }));
}
return true;
}
示例2: InjectData
void InjectData(ModuleDef stubModule, MethodDef method, byte[] data)
{
var dataType = new TypeDefUser("", "DataType", stubModule.CorLibTypes.GetTypeRef("System", "ValueType"));
dataType.Layout = TypeAttributes.ExplicitLayout;
dataType.Visibility = TypeAttributes.NestedPrivate;
dataType.IsSealed = true;
dataType.ClassLayout = new ClassLayoutUser(1, (uint)data.Length);
stubModule.GlobalType.NestedTypes.Add(dataType);
var dataField = new FieldDefUser("DataField", new FieldSig(dataType.ToTypeSig())) {
IsStatic = true,
HasFieldRVA = true,
InitialValue = data,
Access = FieldAttributes.CompilerControlled
};
stubModule.GlobalType.Fields.Add(dataField);
MutationHelper.ReplacePlaceholder(method, arg => {
var repl = new List<Instruction>();
repl.AddRange(arg);
repl.Add(Instruction.Create(OpCodes.Dup));
repl.Add(Instruction.Create(OpCodes.Ldtoken, dataField));
repl.Add(Instruction.Create(OpCodes.Call, stubModule.Import(
typeof(RuntimeHelpers).GetMethod("InitializeArray"))));
return repl.ToArray();
});
}
示例3: ImportAssemblyTypeReferences
void ImportAssemblyTypeReferences(ModuleDef originModule, ModuleDef stubModule)
{
var assembly = stubModule.Assembly;
foreach (var ca in assembly.CustomAttributes) {
if (ca.AttributeType.Scope == originModule)
ca.Constructor = (ICustomAttributeType)stubModule.Import(ca.Constructor);
}
foreach (var ca in assembly.DeclSecurities.SelectMany(declSec => declSec.CustomAttributes)) {
if (ca.AttributeType.Scope == originModule)
ca.Constructor = (ICustomAttributeType)stubModule.Import(ca.Constructor);
}
}