本文整理汇总了C#中Mono.Cecil.TypeDefinition类的典型用法代码示例。如果您正苦于以下问题:C# TypeDefinition类的具体用法?C# TypeDefinition怎么用?C# TypeDefinition使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
TypeDefinition类属于Mono.Cecil命名空间,在下文中一共展示了TypeDefinition类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SwapParameterTypes
private static void SwapParameterTypes(MethodDefinition method,
TypeDefinition targetDependency,
TypeReference interfaceType,
HashSet<MethodReference> modifiedMethods)
{
if (method.IsAbstract || !method.HasBody)
return;
bool modified = false;
var parameters = method.Parameters.Cast<ParameterDefinition>();
foreach (var parameter in parameters)
{
var parameterType = parameter.ParameterType;
if (parameterType != targetDependency)
continue;
parameter.ParameterType = interfaceType;
modified = true;
}
if (!modified)
return;
modifiedMethods.Add(method);
}
示例2: IsCastTo
private static bool IsCastTo(TypeDefinition castTarget, Instruction instruction)
{
if (instruction.OpCode != OpCodes.Castclass) return false;
TypeReference typeReference = (TypeReference)instruction.Operand;
return typeReference.Resolve() == castTarget;
}
示例3: CreateDefaultCtor
/// <summary>
/// Create the Ctor
/// </summary>
private static void CreateDefaultCtor(ReachableContext reachableContext, TypeDefinition type)
{
var typeSystem = type.Module.TypeSystem;
var ctor = new MethodDefinition(".ctor", MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName, typeSystem.Void);
ctor.DeclaringType = type;
var body = new MethodBody(ctor);
body.InitLocals = true;
ctor.Body = body;
// Prepare code
var seq = new ILSequence();
seq.Emit(OpCodes.Nop);
seq.Emit(OpCodes.Ret);
// Append ret sequence
seq.AppendTo(body);
// Update offsets
body.ComputeOffsets();
// Add ctor
type.Methods.Add(ctor);
ctor.SetReachable(reachableContext);
}
示例4: TypeWriterBase
public TypeWriterBase(TypeDefinition typeDefinition, int indentCount, TypeCollection typeCollection, ConfigBase config)
{
this.TypeDefinition = typeDefinition;
this.IndentCount = indentCount;
this.TypeCollection = typeCollection;
this.Config = config;
}
示例5: FindReferencesInType
private IEnumerable<AnalyzerTreeNode> FindReferencesInType(TypeDefinition type)
{
foreach (MethodDefinition method in type.Methods) {
bool found = false;
if (!method.HasBody)
continue;
// ignore chained constructors
// (since object is the root of everything, we can short circuit the test in this case)
if (method.Name == ".ctor" &&
(isSystemObject || analyzedType == type || TypesHierarchyHelpers.IsBaseType(analyzedType, type, false)))
continue;
foreach (Instruction instr in method.Body.Instructions) {
MethodReference mr = instr.Operand as MethodReference;
if (mr != null && mr.Name == ".ctor") {
if (Helpers.IsReferencedBy(analyzedType, mr.DeclaringType)) {
found = true;
break;
}
}
}
method.Body = null;
if (found) {
var node = new AnalyzedMethodTreeNode(method);
node.Language = this.Language;
yield return node;
}
}
}
示例6: GetValues
private void GetValues (TypeDefinition type)
{
values.Clear ();
Type ftype = null;
foreach (FieldDefinition field in type.Fields) {
if (field.IsStatic) {
if (ftype == null)
ftype = field.Constant.GetType ();
ulong value;
if (ftype == typeof (ulong)) {
value = (ulong) field.Constant;
if (value != 0 && !values.Contains (value))
values.Add (value);
} else {
long v = Convert.ToInt64 (field.Constant);
if (v > 0) {
value = (ulong) v;
if (!values.Contains (value))
values.Add (value);
} else if (v < 0) {
values.Clear ();
break;
}
}
}
}
}
示例7: ProcessType
private void ProcessType(bool? assemblyConfigureAwaitValue, TypeDefinition type)
{
if (type.IsCompilerGenerated() && type.IsIAsyncStateMachine())
{
return;
}
var configureAwaitValue = (bool?)type.GetConfigureAwaitAttribute()?.ConstructorArguments[0].Value;
configureAwaitValue = configureAwaitValue ?? assemblyConfigureAwaitValue;
foreach (var method in type.Methods)
{
var localConfigureAwaitValue = (bool?)method.GetConfigureAwaitAttribute()?.ConstructorArguments[0].Value;
var localConfigWasSet = localConfigureAwaitValue.HasValue;
localConfigureAwaitValue = localConfigureAwaitValue ?? configureAwaitValue;
if (localConfigureAwaitValue == null)
continue;
var asyncStateMachineType = method.GetAsyncStateMachineType();
if (asyncStateMachineType != null)
{
AddAwaitConfigToAsyncMethod(asyncStateMachineType, localConfigureAwaitValue.Value);
}
else if (localConfigWasSet)
{
LogWarning($"ConfigureAwaitAttribue applied to non-async method '{method.FullName}'.");
continue;
}
}
}
示例8: CheckType
public RuleResult CheckType (TypeDefinition type)
{
// rule applies only to types, interfaces and structures (value types)
if (type.IsEnum || type.IsDelegate () || type.IsGeneratedCode ())
return RuleResult.DoesNotApply;
// rule onyly applies to type that implements IDisposable
if (!type.Implements ("System.IDisposable"))
return RuleResult.DoesNotApply;
// no problem is a finalizer is found
if (type.HasMethod (MethodSignatures.Finalize))
return RuleResult.Success;
// otherwise check for native types
foreach (FieldDefinition field in type.Fields) {
// we can't dispose static fields in IDisposable
if (field.IsStatic)
continue;
if (!field.FieldType.GetElementType ().IsNative ())
continue;
Runner.Report (field, Severity.High, Confidence.High);
}
// special case: a struct cannot define a finalizer so it's a
// bad candidate to hold references to unmanaged resources
if (type.IsValueType && (Runner.CurrentRuleResult == RuleResult.Failure))
Runner.Report (type, Severity.High, Confidence.High, Struct);
return Runner.CurrentRuleResult;
}
示例9: VisitType
public void VisitType(TypeDefinition type)
{
Log.DebugLine(this, "-----------------------------------");
Log.DebugLine(this, "{0}", type);
string names = string.Empty;
foreach (PropertyDefinition prop in type.Properties)
{
string name = prop.Name;
MethodDefinition[] methods1 = type.Methods.GetMethod("Get" + name);
MethodDefinition[] methods2 = type.Methods.GetMethod("Set" + name);
if (methods1.Length > 0 && methods1[0].IsPublic)
names += name + " ";
else if (methods2.Length > 0 && methods2[0].IsPublic)
names += name + " ";
}
if (names.Length > 0)
{
Log.DebugLine(this, "names: {0}", names);
Reporter.TypeFailed(type, CheckID, "Properties: " + names);
}
}
示例10: CheckType
public MessageCollection CheckType(TypeDefinition type, Runner runner)
{
MessageCollection messageCollection = new MessageCollection();
foreach (MethodDefinition method in type.Methods)
{
if (!method.IsStatic)
{
return null;
}
}
foreach (FieldDefinition field in type.Fields)
{
if (!field.IsStatic)
{
return null;
}
}
foreach (MethodDefinition ctor in type.Constructors)
{
if (!ctor.IsStatic && (ctor.Attributes & MethodAttributes.Public) == MethodAttributes.Public)
{
Location location = new Location(type.Name, ctor.Name, 0);
Message message = new Message(MessageString, location, MessageType.Error);
messageCollection.Add(message);
}
}
return messageCollection.Count > 0 ? messageCollection : null;
}
示例11: OfType
public IEnumerable<TypeReference> OfType(TypeDefinition definition)
{
if (definition == null)
throw new ArgumentNullException("definition");
return definition.Methods.SelectMany(m => OfMethod(m));
}
示例12: CheckType
public RuleResult CheckType (TypeDefinition type)
{
//type does not apply if not an interface or is an empty interface
if (!type.IsInterface || !type.HasMethods)
return RuleResult.DoesNotApply;
//TODO: take into account [InternalsVisibleTo] on iface's assembly
AssemblyDefinition current_assembly = type.Module.Assembly;
if (type.IsVisible ()) {
// We should not, by default, promote the implementation of interfaces in assemblies that
// do not, already, refer to the current one because:
// (a) we could be suggesting circular references (solvable, or not, by refactoring)
// (b) it has a very HIGH performance cost, with verry LITTLE value (in # of defects)
string current_assembly_name = current_assembly.Name.Name;
foreach (AssemblyDefinition assembly in Runner.Assemblies) {
// by default only process assemblies (from the set) that refers to the current one
// or the current one itself
if (!ReferencesOnly || (current_assembly_name == assembly.Name.Name) ||
assembly.References (current_assembly_name)) {
CheckAssemblyTypes (assembly, type);
}
}
} else {
// if the interface is not visible then we only check this assembly
CheckAssemblyTypes (current_assembly, type);
}
return Runner.CurrentRuleResult;
}
示例13: addMainMethod
public MethodDefinition addMainMethod(TypeDefinition tdTargetType)
{
MethodDefinition mainMethod = createMethod_StaticVoid("Main");
tdTargetType.Methods.Add(mainMethod);
assemblyDefinition.EntryPoint = mainMethod;
return mainMethod;
}
示例14: addType
public TypeDefinition addType(String sTypeNameSpace, String sTypeName, TypeAttributes taTypeAttributes,
TypeReference trTypeReference)
{
var tdNewType = new TypeDefinition(sTypeName, sTypeNameSpace, taTypeAttributes, trTypeReference);
mainModule.Types.Add(tdNewType);
return tdNewType;
}
示例15: GetInjecteeCecilType
internal virtual TypeDefinition GetInjecteeCecilType(ModuleDefinition module)
{
if (_injecteeCecilDef == null)
_injecteeCecilDef = module.FindMatchingType(InjecteeType, true);
return _injecteeCecilDef;
}