本文整理汇总了C#中Mono.Cecil.TypeDefinition.Implements方法的典型用法代码示例。如果您正苦于以下问题:C# TypeDefinition.Implements方法的具体用法?C# TypeDefinition.Implements怎么用?C# TypeDefinition.Implements使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Mono.Cecil.TypeDefinition
的用法示例。
在下文中一共展示了TypeDefinition.Implements方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CheckType
public RuleResult CheckType (TypeDefinition type)
{
// if type is not serializable or has not any fields or does not implements a custom serialization
if (!type.IsSerializable || !type.HasFields || type.Implements ("System.Runtime.Serialization", "ISerializable"))
return RuleResult.DoesNotApply;
foreach (FieldDefinition field in type.Fields) {
if (!field.IsNotSerialized && !field.IsStatic) {
TypeDefinition fieldType = field.FieldType.Resolve ();
if (fieldType == null)
continue;
if (fieldType.IsInterface) {
string msg = String.Format (CultureInfo.InvariantCulture,
"Serialization of interface {0} as field {1} unknown until runtime",
fieldType, field.Name);
Runner.Report (field, Severity.Critical, Confidence.Low, msg);
continue;
}
if (!fieldType.IsEnum && !fieldType.IsSerializable) {
string msg = String.Format (CultureInfo.InvariantCulture,
"The field {0} isn't serializable.", field.Name);
Runner.Report (field, Severity.Critical, Confidence.High, msg);
}
}
}
return Runner.CurrentRuleResult;
}
示例2: CheckType
public RuleResult CheckType (TypeDefinition type)
{
if (type.IsAbstract || !type.HasMethods || !type.Implements ("Gendarme.Framework", "IRule"))
return RuleResult.DoesNotApply;
foreach (MethodDefinition method in type.Methods) {
if (method.IsConstructor || !method.HasBody || !OpCodeBitmask.Calls.Intersect (OpCodeEngine.GetBitmask (method)))
continue;
foreach (Instruction instruction in method.Body.Instructions) {
if (instruction.OpCode.FlowControl != FlowControl.Call)
continue;
MethodReference m = (instruction.Operand as MethodReference);
if (m == null || (m.Name != "Report"))
continue;
if (m.DeclaringType.IsNamed ("Gendarme.Framework", "IRunner"))
return RuleResult.Success;
}
}
// we have not found a call, so report failure
Runner.Report (type, Severity.High, Confidence.Normal);
return RuleResult.Failure;
}
示例3: CheckType
public RuleResult CheckType (TypeDefinition type)
{
// rule does not apply to interface (since [Serializable] is not applicable to interfaces)
// nor does it apply to delegates
if (type.IsInterface || type.IsDelegate ())
return RuleResult.DoesNotApply;
// rule does not apply if the type does not implements ISerializable
if (!type.Implements ("System.Runtime.Serialization", "ISerializable"))
return RuleResult.DoesNotApply;
// rule applies only if base type is serializable
if (!type.BaseType.IsNamed ("System", "Object")) {
TypeDefinition base_type = type.BaseType.Resolve ();
// in doubt don't report
if ((base_type == null) || !base_type.IsSerializable)
return RuleResult.DoesNotApply;
}
// rule applies, only Success or Failure from the point on
// ok if the type has the [Serializable] pseudo-attribute
if (type.IsSerializable)
return RuleResult.Success;
Runner.Report (type, Severity.Critical, Confidence.Total);
return RuleResult.Failure;
}
示例4: CheckType
public RuleResult CheckType(TypeDefinition type)
{
// rule applies only to types and interfaces
if (type.IsEnum || type.IsDelegate() || type.IsGeneratedCode() ||
type.IsValueType || type.IsInterface)
return RuleResult.DoesNotApply;
// rule only applies to type that implements IDisposable
if (!type.Implements("System", "IDisposable"))
return RuleResult.DoesNotApply;
// rule only applies to type that doesn't derive from class that implements IDisposable
if (type.BaseType.Implements("System", "IDisposable"))
return RuleResult.DoesNotApply;
// rule only applies if type has a Dispose(bool) method
if (!type.HasMethod(Dispose))
return RuleResult.DoesNotApply;
// no problem if a finalizer is found
if (type.HasMethod(MethodSignatures.Finalize))
return RuleResult.Success;
Runner.Report(type, Severity.Medium, Confidence.High);
return RuleResult.Failure;
}
示例5: 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;
}
示例6: AddStubImplementationFor
/// <summary>
/// Implements each one of the methods in the given <paramref name="interfaceType"/>.
/// </summary>
/// <remarks>By default, each implemented method will throw a <see cref="NotImplementedException"/>.</remarks>
/// <param name="interfaceType">The interface type.</param>
/// <param name="type">The host type.</param>
/// <returns>The list of stubbed methods.</returns>
public IEnumerable<MethodDefinition> AddStubImplementationFor(System.Type interfaceType, TypeDefinition type)
{
var module = type.Module;
var interfaceTypeRef = module.Import(interfaceType);
if (!type.Implements(interfaceTypeRef))
type.Interfaces.Add(interfaceTypeRef);
return CreateInterfaceStub(interfaceType, type);
}
示例7: CheckType
public RuleResult CheckType (TypeDefinition type)
{
bool endsWithRule = type.Name.EndsWith ("Rule", StringComparison.Ordinal);
bool implementsIRule = type.Implements ("Gendarme.Framework.IRule");
if (implementsIRule && !endsWithRule)
Runner.Report (type, Severity.Medium, Confidence.High, "Type implements IRule but does not end with the 'Rule'");
else if (!implementsIRule && endsWithRule)
Runner.Report (type, Severity.Medium, Confidence.High, "Type does not implement IRule but ends with the 'Rule'");
return Runner.CurrentRuleResult;
}
示例8: CheckType
public RuleResult CheckType (TypeDefinition type)
{
// that will cover interfaces, delegates too
if (!type.HasFields)
return RuleResult.DoesNotApply;
// rule doesn't apply to enums, interfaces, structs, delegates or generated code
if (type.IsEnum || type.IsValueType || type.IsGeneratedCode ())
return RuleResult.DoesNotApply;
MethodDefinition explicitDisposeMethod = null;
MethodDefinition implicitDisposeMethod = null;
bool abstractWarning = false;
if (type.Implements ("System", "IDisposable")) {
implicitDisposeMethod = type.GetMethod (MethodSignatures.Dispose);
explicitDisposeMethod = type.GetMethod (MethodSignatures.DisposeExplicit);
if (IsAbstract (implicitDisposeMethod) || IsAbstract (explicitDisposeMethod)) {
abstractWarning = true;
} else {
return RuleResult.Success;
}
}
FieldCandidates.Clear ();
foreach (FieldDefinition field in type.Fields) {
// we can't dispose static fields in IDisposable
if (field.IsStatic)
continue;
TypeDefinition fieldType = field.FieldType.GetElementType ().Resolve ();
if (fieldType == null)
continue;
if (FieldTypeIsCandidate (fieldType))
FieldCandidates.Add (field);
}
// if there are fields types that implements IDisposable
if (type.HasMethods && (FieldCandidates.Count > 0)) {
// check if we're assigning new object to them
foreach (MethodDefinition method in type.Methods) {
CheckMethod (method, abstractWarning);
}
}
// Warn about possible confusion if the Dispose methods are abstract
if (IsAbstract (implicitDisposeMethod))
Runner.Report (implicitDisposeMethod, Severity.Medium, Confidence.High, AbstractDisposeMessage);
return Runner.CurrentRuleResult;
}
示例9: CheckType
virtual public RuleResult CheckType (TypeDefinition type)
{
if (type.IsAbstract || type.IsGeneratedCode () || !type.Implements (InterfaceNamespace, InterfaceName))
return RuleResult.DoesNotApply;
signatures = GetMethods ();
propertyNames = GetProperties ();
methodsLeft = signatures.Length;
propertiesLeft = propertyNames.Length;
TypeDefinition baseType = type;
while (methodsLeft > 0 || propertiesLeft > 0) {
ProcessType (baseType);
if (baseType.BaseType == null)
break;
TypeDefinition td = baseType.BaseType.Resolve ();
if (td == null)
break;
baseType = td;
}
if (propertiesLeft > 0) {
foreach (string propertyName in propertyNames) {
if (propertyName == null)
continue;
Runner.Report (type, Severity.Medium, Confidence.High,
"Type does not have strongly typed version of property " + propertyName);
}
}
if (methodsLeft > 0) {
foreach (MethodSignature signature in signatures) {
if (signature == null)
continue;
Runner.Report (type, Severity.Medium, Confidence.High,
"Type does not have strongly typed version of method " + signature.Name);
}
}
return Runner.CurrentRuleResult;
}
示例10: CheckType
public RuleResult CheckType (TypeDefinition type)
{
//if isn't serializable or
//implements a custom serialization
if (!type.IsSerializable || type.Implements (ISerializable))
return RuleResult.DoesNotApply;
foreach (FieldDefinition field in type.Fields) {
if (!field.IsNotSerialized) {
TypeDefinition fieldType = field.FieldType.Resolve ();
if (fieldType.IsInterface) {
Runner.Report (field, Severity.Critical, Confidence.Low, String.Format (InterfaceMessageError, fieldType, field.Name));
continue;
}
if (!fieldType.IsEnum && !fieldType.IsSerializable)
Runner.Report (field, Severity.Critical, Confidence.High, String.Format (MessageError, field.Name));
}
}
return Runner.CurrentRuleResult;
}
示例11: CheckType
public RuleResult CheckType (TypeDefinition type)
{
// rule does not apply to enums, interfaces and generated code
if (type.IsEnum || type.IsInterface || type.IsGeneratedCode ())
return RuleResult.DoesNotApply;
// rule applies only to visible types
if (!type.IsVisible ())
return RuleResult.DoesNotApply;
// rule only applies if the type implements IEnumerable
if (!type.Implements ("System.Collections", "IEnumerable"))
return RuleResult.DoesNotApply;
// rule does not apply to the types implementing IDictionary
if (type.Implements ("System.Collections", "IDictionary"))
return RuleResult.DoesNotApply;
// the type should implement IEnumerable<T> too
if (!type.Implements ("System.Collections.Generic", "IEnumerable`1"))
Runner.Report (type, Severity.Medium, Confidence.High);
return Runner.CurrentRuleResult;
}
示例12: CheckType
public RuleResult CheckType (TypeDefinition type)
{
// rule does not apply to enums, delegates and to generated code
if (type.IsEnum || type.IsDelegate () || type.IsGeneratedCode ())
return RuleResult.DoesNotApply;
// rule applies only if the type overrides Equals(object)
if (!type.HasMethod (MethodSignatures.Equals))
return RuleResult.DoesNotApply;
// if so then the type should also implement Equals(type) since this avoid a cast
// operation (for reference types) and also boxing (for value types).
// we suggest to implement IEquatable<T> if
// * the assembly targets the 2.0 (or later) runtime
// * and it does not already implement it
if (type.Module.Runtime >= TargetRuntime.Net_2_0) {
if (!type.Implements ("System", "IEquatable`1")) {
Runner.Report (type, Severity.Medium, Confidence.Total, "Implement System.IEquatable<T>");
}
return Runner.CurrentRuleResult;
}
parameters [0] = type.GetFullName ();
if (type.GetMethod (MethodAttributes.Public, "Equals", "System.Boolean", parameters) != null)
return RuleResult.Success;
// we consider this a step more severe for value types since it will need
// boxing/unboxing with Equals(object)
Severity severity = type.IsValueType ? Severity.Medium : Severity.Low;
string msg = String.Format (CultureInfo.InvariantCulture, "Implement 'bool Equals({0})'", type.Name);
Runner.Report (type, severity, Confidence.High, msg);
return RuleResult.Failure;
}
示例13: CheckType
public RuleResult CheckType (TypeDefinition type)
{
if (type.IsInterface || type.IsEnum || type.IsDelegate ())
return RuleResult.DoesNotApply;
if (type.Implements ("System", "IDisposable")) {
Log.WriteLine (this);
Log.WriteLine (this, "----------------------------------");
Log.WriteLine (this, type);
MethodDefinition dispose0 = null;
MethodDefinition dispose1 = null;
FindDisposeMethods (type, ref dispose0, ref dispose1);
// The compiler will normally require that the type have a declaration for
// Dispose unless the base class also implements IDisposable. In that
// case we'll ignore any defects because the type doesn't actually
// implement IDisposable.
if (dispose0 != null || dispose1 != null) {
CheckDispose0 (dispose0);
CheckDispose1 (type, dispose1);
}
}
return Runner.CurrentRuleResult;
}
示例14: CheckType
public RuleResult CheckType (TypeDefinition type)
{
if (!type.IsSerializable || !type.Implements ("System.Runtime.Serialization", "ISerializable"))
return RuleResult.DoesNotApply;
MethodDefinition getObjectData = type.GetMethod (MethodSignatures.GetObjectData);
if (getObjectData == null) {
// no GetObjectData means that the type's ancestor does the job but
// are we introducing new instance fields that need to be serialized ?
if (!type.HasFields)
return RuleResult.Success;
// there are some, but they could be static
foreach (FieldDefinition field in type.Fields) {
if (!field.IsStatic)
Runner.Report (field, Severity.Medium, Confidence.High);
}
} else {
if (type.HasFields)
CheckUnusedFieldsIn (type, getObjectData);
if (!type.IsSealed && getObjectData.IsFinal) {
string msg = "Either seal this type or change GetObjectData method to be virtual";
Runner.Report (getObjectData, Severity.High, Confidence.Total, msg);
}
}
return Runner.CurrentRuleResult;
}
示例15: CheckType
public RuleResult CheckType (TypeDefinition type)
{
// rule applies only to types that implements ISerializable
if (!type.Implements ("System.Runtime.Serialization.ISerializable"))
return RuleResult.DoesNotApply;
MethodDefinition method = type.GetMethod (MethodSignatures.GetObjectData);
if (method == null)
return RuleResult.DoesNotApply;
// *** ok, the rule applies! ***
// is there any security applied ?
if (!method.HasSecurityDeclarations) {
Runner.Report (method, Severity.High, Confidence.Total, NotFound);
return RuleResult.Failure;
}
// the SerializationFormatter must be a subset of the one (of the) demand(s)
bool demand = false;
foreach (SecurityDeclaration declsec in method.SecurityDeclarations) {
switch (declsec.Action) {
case Mono.Cecil.SecurityAction.Demand:
case Mono.Cecil.SecurityAction.NonCasDemand:
case Mono.Cecil.SecurityAction.LinkDemand:
case Mono.Cecil.SecurityAction.NonCasLinkDemand:
demand = true;
if (!RuleSet.IsSubsetOf (declsec.ToPermissionSet ())) {
string message = String.Format ("{0} is not a subset of {1} permission set",
"SerializationFormatter", declsec.Action);
Runner.Report (method, Severity.High, Confidence.Total, message);
}
break;
}
}
// there was no [NonCas][Link]Demand but other actions are possible
if (!demand)
Runner.Report (method, Severity.High, Confidence.Total, NotFound);
return Runner.CurrentRuleResult;
}