本文整理汇总了C#中Mono.Cecil.TypeDefinition.HasInterface方法的典型用法代码示例。如果您正苦于以下问题:C# TypeDefinition.HasInterface方法的具体用法?C# TypeDefinition.HasInterface怎么用?C# TypeDefinition.HasInterface使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Mono.Cecil.TypeDefinition
的用法示例。
在下文中一共展示了TypeDefinition.HasInterface方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WeaveTypeProperties
protected void WeaveTypeProperties(TypeDefinition type)
{
var properties = type.Properties.Where(
p => p.CustomAttributes.Any(
a => a.AttributeType.FullName == typeof (PropertyAttribute).FullName));
if(!properties.Any())
return;
if(!type.HasInterface(typeof(IPropertyNotifier).FullName))
throw new Exception(string.Format("Type {0} has properties but does not implement {1}", type.FullName, typeof(IPropertyNotifier).FullName));
Console.WriteLine(type.FullName);
var weaver = new TypeWeaver(originType, type);
foreach(var property in properties)
{
weaver.NamePrefix = "<$" + property.Name + "$>";
var desNotifierMethod = type.GetMethod(tNotifierMethod.Name);
weaver.Map(tNotifierMethod, desNotifierMethod);
var propField = weaver.AddDefinition(tPropertyField);
propField.FieldType = property.PropertyType;
weaver.AddConstant(tPropertyNameC,property.Name);
//TODO delete property generated field
weaver.Merge(tPropertyProperty, property);
weaver.ProcessInstructions();
}
}
示例2: EnsureTypeImplementsIDisposable
/// <summary>
/// Method verifies that the type implements IDisposable and emits
/// IL to implement it if necessary.
/// </summary>
/// <param name="type">The type that needs to be checked and potentially modified.</param>
private void EnsureTypeImplementsIDisposable(TypeDefinition type)
{
// Verify whether the class already implements IDisposable. If not then
// implement it.
if (!type.HasInterface("System.IDisposable"))
{
type.Interfaces.Add(type.Module.ImportReference(typeof(IDisposable)));
// Verify whether the type already has a public Dipose method. If not then add a shell.
if (!type.HasDisposeMethod())
{
var disposeMethodAttribs =
Mono.Cecil.MethodAttributes.Public |
Mono.Cecil.MethodAttributes.Final |
Mono.Cecil.MethodAttributes.HideBySig |
Mono.Cecil.MethodAttributes.NewSlot |
Mono.Cecil.MethodAttributes.Virtual;
var disposeDefinition =
new MethodDefinition("Dispose", disposeMethodAttribs, ModuleDefinition.TypeSystem.Void);
// Empty method shell that only returns void.
disposeDefinition.Body.Instructions.Add(Instruction.Create(OpCodes.Nop));
disposeDefinition.Body.Instructions.Add(Instruction.Create(OpCodes.Ret));
type.Methods.Add(disposeDefinition);
}
}
}