本文整理汇总了C#中Mono.Cecil.ModuleDefinition.Import方法的典型用法代码示例。如果您正苦于以下问题:C# ModuleDefinition.Import方法的具体用法?C# ModuleDefinition.Import怎么用?C# ModuleDefinition.Import使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Mono.Cecil.ModuleDefinition
的用法示例。
在下文中一共展示了ModuleDefinition.Import方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Initialize
internal static void Initialize(ModuleDefinition moduleDefinition)
{
ModuleDefinition = moduleDefinition;
typeType = ModuleDefinition.Import(typeof(Type));
taskType = ModuleDefinition.Import(typeof(Task));
getTypeFromRuntimeHandleMethod = ModuleDefinition.Import(typeType.Resolve().Methods.Single(x => x.Name == "GetTypeFromHandle"));
typeGetMethod = ModuleDefinition.Import(typeType.Resolve().Methods.Single(x => x.Name == "GetMethod" && x.Parameters.Count == 5));
taskTType = ModuleDefinition.Import(typeof(Task<>));
taskFromResult = ModuleDefinition.Import(taskType.Resolve().Methods.Single(x => x.Name == "FromResult"));
}
示例2: AddAttribute
/// <summary>
/// Adds the specified attribute to the specified method.
/// </summary>
/// <param name="field">The method to add the attribute to.</param>
/// <param name="attribute">The attribute to add.</param>
/// <param name="module">The module the field is defined in.</param>
public static void AddAttribute(MethodDefinition method, Type attribute, ModuleDefinition module)
{
TypeDefinition tr = module.Import(attribute).Resolve();
MethodDefinition mr = tr.Methods.First(value => value.IsConstructor);
MethodReference rf = module.Import(mr);
method.CustomAttributes.Add(new CustomAttribute(rf));
}
示例3: Convert
public Either<MemberReference, PropertyReferenceContainer> Convert(IAstReference astReference, ModuleDefinition module)
{
var reflectedType = astReference as AstReflectedType;
if (reflectedType != null)
return module.Import(reflectedType.ActualType);
var reflectedMethod = astReference as AstReflectedMethod;
if (reflectedMethod != null)
return module.Import(reflectedMethod.Method);
var reflectedConstructor = astReference as AstReflectedConstructor;
if (reflectedConstructor != null)
return module.Import(reflectedConstructor.Constructor);
var reflectedProperty = astReference as AstReflectedProperty;
if (reflectedProperty != null) {
var getMethod = reflectedProperty.Property.GetGetMethod();
var setMethod = reflectedProperty.Property.GetSetMethod();
return new PropertyReferenceContainer(
getMethod != null ? module.Import(getMethod) : null,
setMethod != null ? module.Import(setMethod) : null
);
}
return null;
}
示例4: AddMixing
public void AddMixing(ModuleDefinition targetModule)
{
var objectType = targetModule.Import(typeof(object)).Resolve();
var mixesAttributeBaseType = targetModule.Import(typeof(MixesAttributeBase)).Resolve();
var originalTypes = new List<TypeDefinition>(targetModule.Types);
foreach (var type in originalTypes)
{
if(type.CustomAttributes.Any(
customAttribute =>
{
var attributeType = customAttribute.AttributeType.Resolve();
while (attributeType != objectType &&
attributeType != mixesAttributeBaseType &&
attributeType.BaseType != null)
{
attributeType = attributeType.BaseType.Resolve();
}
return attributeType == mixesAttributeBaseType;
}))
{
this.AddIMixes(type);
this.AddISerializable(type);
}
}
}
示例5: CreateCustomAttributeType
protected override TypeDefinition CreateCustomAttributeType(ModuleDefinition targetModule)
{
ArgumentUtility.CheckNotNull ("targetModule", targetModule);
var customType = new TypeDefinition (_attributeNamespace,
_attributeName,
TypeAttributes.Public | TypeAttributes.Class,
targetModule.Import (typeof (Attribute)));
var ctor = new MethodDefinition (
".ctor",
MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName,
targetModule.Import (typeof (void)));
ctor.HasThis = true;
var il = ctor.Body.GetILProcessor ();
il.Emit (OpCodes.Ldarg_0);
il.Emit (
OpCodes.Call,
targetModule.Import (typeof (Attribute).GetConstructor (BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null)));
il.Emit (OpCodes.Ret);
customType.Methods.Add (ctor);
return customType;
}
示例6: Hooker
public Hooker(ModuleDefinition module)
{
Module = module;
hookRegistryType = Module.Import(typeof(Hooks.HookRegistry));
rmhType = Module.Import(typeof(System.RuntimeMethodHandle));
onCallMethod = Module.Import(
typeof(Hooks.HookRegistry).GetMethods()
.First(mi => mi.Name.Contains("OnCall")));
Module.AssemblyReferences.Add(new AssemblyNameReference("HookRegistry", new Version(1, 0, 0, 0)));
}
示例7: CopyTo
void CopyTo(CustomAttribute target, ModuleDefinition context)
{
foreach (var arg in ConstructorArguments)
target.ConstructorArguments.Add(new CustomAttributeArgument(context.Import(arg.Type), arg.Value));
foreach (var field in Fields)
target.Fields.Add(new CustomAttributeNamedArgument(field.Name, new CustomAttributeArgument(context.Import(field.Argument.Type), field.Argument.Value)));
foreach (var prop in Properties)
target.Properties.Add(new CustomAttributeNamedArgument(prop.Name, new CustomAttributeArgument(context.Import(prop.Argument.Type), prop.Argument.Value)));
}
示例8: TypeReferenceProvider
public TypeReferenceProvider(TraceLoggingConfiguration configuration, ILoggerAdapterMetadataScopeProvider loggerAdapterMetadataScopeProvider, ModuleDefinition moduleDefinition)
{
_configuration = configuration;
_moduleDefinition = moduleDefinition;
_loggerAdapterMetadataScopeProvider = loggerAdapterMetadataScopeProvider;
_stringArray = new Lazy<TypeReference>(() => moduleDefinition.Import((typeof(string[]))));
_objectArray = new Lazy<TypeReference>(() => moduleDefinition.Import(typeof(object[])));
_type = new Lazy<TypeReference>(() => moduleDefinition.Import(typeof(Type)));
_stopwatch = new Lazy<TypeReference>(() => moduleDefinition.Import(typeof(Stopwatch)));
_exception = new Lazy<TypeReference>(() => moduleDefinition.Import(typeof(Exception)));
}
示例9: Construct
/// <summary>
/// Constructs a type that implements the
/// <see cref="IProxy"/> interface.
/// </summary>
/// <param name="module">The module that will hold the target type.</param>
/// <param name="targetType">The type that will implement the <see cref="IProxy"/> interface.</param>
public void Construct(ModuleDefinition module, TypeDefinition targetType)
{
TypeReference proxyInterfaceType = module.Import(typeof (IProxy));
TypeReference interceptorType = module.Import(typeof (IInterceptor));
// Implement the IProxy interface only once
if (targetType.Interfaces.Contains(proxyInterfaceType))
return;
targetType.Interfaces.Add(proxyInterfaceType);
targetType.AddProperty("Interceptor", interceptorType);
}
示例10: ProcessType
static void ProcessType(ModuleDefinition moduleDefinition, TypeDefinition type)
{
foreach (var toRemove in type.Properties.Where(x => x.IsCompilerGenerated()).ToList())
{
type.Properties.Remove(toRemove);
}
foreach (var toRemove in type.Methods.Where(x => x.IsCompilerGenerated() && !x.Name.StartsWith("get_") && !x.Name.StartsWith("set_")).ToList())
{
type.Methods.Remove(toRemove);
}
foreach (var toRemove in type.Fields.Where(x => x.IsCompilerGenerated()).ToList())
{
type.Fields.Remove(toRemove);
}
foreach (var property in type.Properties)
{
property.RemoveUnwantedAttributes();
}
foreach (var field in type.Fields)
{
field.RemoveUnwantedAttributes();
}
var exceptionReference = new TypeReference("System", "Exception", moduleDefinition.TypeSystem.String.Module, moduleDefinition.TypeSystem.String.Scope);
exceptionReference = moduleDefinition.Import(exceptionReference);
var ctor = moduleDefinition.Import(exceptionReference.Resolve().GetConstructors().First(c => !c.HasParameters));
foreach (var method in type.Methods)
{
method.RemoveUnwantedAttributes();
if (method.HasBody)
{
//todo: preserve a single pdb line
var body = method.Body;
var validSequencePoint = method.GetValidSequencePoint();
body.Variables.Clear();
body.ExceptionHandlers.Clear();
body.Instructions.Clear();
body.Instructions.Add(Instruction.Create(OpCodes.Newobj, ctor));
var instruction = Instruction.Create(OpCodes.Throw);
if (validSequencePoint != null)
{
instruction.SequencePoint = validSequencePoint;
}
body.Instructions.Add(instruction);
}
}
}
示例11: ReferenceContainer
public ReferenceContainer(ModuleDefinition moduleDefinition, IAssemblyResolver assemblyResolver)
{
var systemDefinition = assemblyResolver.Resolve("mscorlib");
var yalfDefinition = assemblyResolver.Resolve("Yalf");
var yalfTypes = yalfDefinition.MainModule.Types;
var logType = yalfTypes.Single(x => x.Name == "Log");
var iContextType = yalfTypes.Single(x => x.Name == "IContext");
var iDisposableType = systemDefinition.MainModule.Types.Single(x => x.Name == "IDisposable");
var exceptionType = systemDefinition.MainModule.Types.Single(x => x.Name == "Exception");
MethodContextMethod = moduleDefinition.Import(logType.Methods.Single(m => m.Name == "MethodContext"));
TraceExceptionMethod = moduleDefinition.Import(logType.Methods.Single(m => m.Name == "TraceException"));
CreateRecordReturnMethod = retType => {
var recordReturn = moduleDefinition.Import(iContextType.Methods.Single(m => m.Name == "RecordReturn"));
if (retType.IsGenericInstance)
{
return recordReturn.MakeGeneric(retType);
}
else
{
return recordReturn.MakeGeneric(retType);
}
};
DisposeMethod = moduleDefinition.Import(iDisposableType.Methods.Single(m => m.Name == "Dispose"));
LogType = moduleDefinition.Import(logType);
IContextType = moduleDefinition.Import(iContextType);
ExceptionType = moduleDefinition.Import(exceptionType);
PreserveStackTraceMethod = moduleDefinition.Import(iContextType.Methods.Single(m => m.Name == "PreserveStackTrace"));
}
示例12: ResolveGenericParameters
public static MethodReference ResolveGenericParameters(this MethodReference self, TypeReference declaringTypeRef,
ModuleDefinition module)
{
if (self == null)
throw new ArgumentNullException(nameof(self));
if (declaringTypeRef == null)
throw new ArgumentNullException(nameof(declaringTypeRef));
var reference = new MethodReference(self.Name, self.ReturnType)
{
DeclaringType = declaringTypeRef,
HasThis = self.HasThis,
ExplicitThis = self.ExplicitThis,
CallingConvention = self.CallingConvention
};
foreach (var parameter in self.Parameters) {
var p = parameter.ParameterType.IsGenericParameter ? parameter.ParameterType : module.Import(parameter.ParameterType);
reference.Parameters.Add(new ParameterDefinition(p));
}
foreach (var generic_parameter in self.GenericParameters)
reference.GenericParameters.Add(new GenericParameter(generic_parameter.Name, reference));
return reference;
}
示例13: CecilVisitor
public CecilVisitor(AssemblyDefinition assembly, ModuleDefinition module)
{
_assembly = assembly;
_module = module;
var program = _module.Types.First(t => t.Name == "Program");
_main = program.Methods.First(m => m.Name == "Main");
_body = _main.Body;
_instructions = _body.Instructions;
_instructions.Clear();
_objCtor = _module.Import(typeof(object).GetConstructor(new System.Type[0]));
var systemFunctions = _module.Types.First(t => t.Name == "SystemFunctions");
// Math methods
_fact = systemFunctions.Methods.First(m => m.Name == "Fact");
_max = systemFunctions.Methods.First(m => m.Name == "Max");
_min = systemFunctions.Methods.First(m => m.Name == "Min");
_pow = systemFunctions.Methods.First(m => m.Name == "Pow");
// Console methods
_printBool = systemFunctions.Methods.First(m => m.Name == "PrintBool");
_printInt = systemFunctions.Methods.First(m => m.Name == "PrintInt");
_userFunctions = _module.Types.First(t => t.Name == "UserFunctions");
}
示例14: Clone
public static CustomAttribute Clone(CustomAttribute custattr, ModuleDefinition context,
IImportMapper mapper)
{
var ca = new CustomAttribute(context.Import(custattr.Constructor, mapper));
custattr.CopyTo(ca, context, mapper);
return ca;
}
示例15: Convert
public Either<MemberReference, PropertyReferenceContainer> Convert(IAstReference astReference, ModuleDefinition module, IReferenceProvider recursive)
{
if (astReference != AstVoidType.Instance)
return null;
return module.Import(typeof(void));
}