本文整理汇总了C#中TypeDefinition类的典型用法代码示例。如果您正苦于以下问题:C# TypeDefinition类的具体用法?C# TypeDefinition怎么用?C# TypeDefinition使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
TypeDefinition类属于命名空间,在下文中一共展示了TypeDefinition类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: InjectAsStreamReader
void InjectAsStreamReader(TypeDefinition targetType, FieldDefinition fieldDefinition)
{
AsStreamReaderMethod = new MethodDefinition("AsStreamReader", staticMethodAttributes, StreamReaderTypeReference);
var streamVariable = new VariableDefinition(StreamTypeReference);
AsStreamReaderMethod.Body.Variables.Add(streamVariable);
var pathParam = new ParameterDefinition(ModuleDefinition.TypeSystem.String);
AsStreamReaderMethod.Parameters.Add(pathParam);
AsStreamReaderMethod.Body.InitLocals = true;
var inst = AsStreamReaderMethod.Body.Instructions;
var skipReturn = Instruction.Create(OpCodes.Nop);
inst.Add(Instruction.Create(OpCodes.Ldsfld, fieldDefinition));
inst.Add(Instruction.Create(OpCodes.Ldarg, pathParam));
inst.Add(Instruction.Create(OpCodes.Callvirt, GetManifestResourceStreamMethod));
inst.Add(Instruction.Create(OpCodes.Stloc, streamVariable));
inst.Add(Instruction.Create(OpCodes.Ldloc, streamVariable));
inst.Add(Instruction.Create(OpCodes.Brtrue_S, skipReturn));
inst.Add(Instruction.Create(OpCodes.Ldnull));
inst.Add(Instruction.Create(OpCodes.Ret));
inst.Add(skipReturn);
inst.Add(Instruction.Create(OpCodes.Ldloc, streamVariable));
inst.Add(Instruction.Create(OpCodes.Newobj, StreamReaderConstructorReference));
inst.Add(Instruction.Create(OpCodes.Ret));
targetType.Methods.Add(AsStreamReaderMethod);
}
示例2: FindGetLogger
void FindGetLogger(TypeDefinition typeDefinition)
{
if (!typeDefinition.IsPublic)
{
var message = $"The logger factory type '{typeDefinition.FullName}' needs to be public.";
throw new WeavingException(message);
}
GetLoggerMethod = typeDefinition
.Methods
.FirstOrDefault(x =>
x.Name == "GetLogger" &&
x.IsStatic &&
x.HasGenericParameters &&
x.GenericParameters.Count == 1 &&
x.Parameters.Count == 0);
if (GetLoggerMethod == null)
{
throw new WeavingException("Found 'LoggerFactory' but it did not have a static 'GetLogger' method that takes 'string' as a parameter");
}
if (!GetLoggerMethod.Resolve().IsPublic)
{
var message = $"The logger factory method '{GetLoggerMethod.FullName}' needs to be public.";
throw new WeavingException(message);
}
}
示例3: Execute
public void Execute()
{
var validationTemplateAttribute = ModuleDefinition
.Assembly
.CustomAttributes
.FirstOrDefault(x=>x.AttributeType.Name == "ValidationTemplateAttribute");
if (validationTemplateAttribute == null)
{
LogInfo("Could not find a 'ValidationTemplateAttribute' on the current assembly. Going to search current assembly for 'ValidationTemplate'.");
TypeDefinition = ModuleDefinition
.GetTypes()
.FirstOrDefault(x =>
x.Name == "ValidationTemplate" ||
x.Name == "ValidationTemplate`1");
if (TypeDefinition == null)
{
throw new WeavingException("Could not find a type named 'ValidationTemplate'");
}
TypeReference = TypeDefinition;
FindConstructor();
}
else
{
var typeReference = (TypeReference) validationTemplateAttribute.ConstructorArguments.First().Value;
TypeReference = typeReference;
TypeDefinition = typeReference.Resolve();
FindConstructor();
TemplateConstructor = ModuleDefinition.Import(TemplateConstructor);
}
}
示例4: FindIsChangedEventInvokerMethodDefinition
bool FindIsChangedEventInvokerMethodDefinition(TypeDefinition type, out MethodDefinition methodDefinition)
{
//todo: check bool type
methodDefinition = null;
var propertyDefinition = type.Properties
.FirstOrDefault(x =>
x.Name == isChangedPropertyName &&
x.SetMethod != null &&
x.SetMethod.IsPublic
);
if (propertyDefinition != null)
{
if (propertyDefinition.PropertyType.FullName != ModuleDefinition.TypeSystem.Boolean.FullName)
{
LogWarning(string.Format("Found '{0}' but is was of type '{1}' instead of '{2}' so it will not be used.", propertyDefinition.GetName(), propertyDefinition.PropertyType.Name, ModuleDefinition.TypeSystem.Boolean.Name));
return false;
}
if (propertyDefinition.SetMethod.IsStatic)
{
throw new WeavingException(string.Format("Found '{0}' but is was static. Change it to non static.", propertyDefinition.GetName()));
}
methodDefinition = propertyDefinition.SetMethod;
}
return methodDefinition != null;
}
示例5: RecursiveFindMethod
public EventInvokerMethod RecursiveFindMethod(TypeDefinition typeDefinition)
{
var typeDefinitions = new Stack<TypeDefinition>();
MethodDefinition methodDefinition;
var currentTypeDefinition = typeDefinition;
do
{
typeDefinitions.Push(currentTypeDefinition);
if (FindEventInvokerMethodDefinition(currentTypeDefinition, out methodDefinition))
{
break;
}
var baseType = currentTypeDefinition.BaseType;
if (baseType == null || baseType.FullName == "System.Object")
{
return null;
}
currentTypeDefinition = typeResolver.Resolve(baseType);
} while (true);
return new EventInvokerMethod
{
MethodReference = methodGenerifier.GetMethodReference(typeDefinitions, methodDefinition),
IsBeforeAfter = IsBeforeAfterMethod(methodDefinition),
};
}
示例6: ProcessType
public void ProcessType(TypeDefinition typeDefinition)
{
if (IsCompilerGenerated(typeDefinition.CustomAttributes))
{
return;
}
if (typeDefinition.IsNotPublic)
{
if (typeDefinition.IsNested)
{
typeDefinition.IsNestedPublic = true;
}
else
{
typeDefinition.IsPublic = true;
}
AddEditorBrowsableAttribute(typeDefinition.CustomAttributes);
}
if (typeDefinition.IsInterface)
{
return;
}
foreach (var method in typeDefinition.Methods)
{
ProcessMethod(method);
}
foreach (var field in typeDefinition.Fields)
{
ProcessField(field);
}
}
示例7: TryGetField
static FieldDefinition TryGetField(TypeDefinition typeDefinition, PropertyDefinition property)
{
var propertyName = property.Name;
var fieldsWithSameType = typeDefinition.Fields.Where(x => x.DeclaringType == typeDefinition).ToList();
foreach (var field in fieldsWithSameType)
{
//AutoProp
if (field.Name == $"<{propertyName}>k__BackingField")
{
return field;
}
}
foreach (var field in fieldsWithSameType)
{
//diffCase
var upperPropertyName = propertyName.ToUpper();
var fieldUpper = field.Name.ToUpper();
if (fieldUpper == upperPropertyName)
{
return field;
}
//underScore
if (fieldUpper == "_" + upperPropertyName)
{
return field;
}
}
return GetSingleField(property);
}
示例8: IsSecurityCritical
public bool IsSecurityCritical (TypeDefinition type)
{
string entry = type.GetFullName ();
if (critical.Contains (entry))
return true;
return type.IsNested ? IsSecurityCritical (type.DeclaringType) : false;
}
示例9: HasCorrectMethods
private static bool HasCorrectMethods(TypeDefinition type)
{
return type.Methods.Any(IsOnEntryMethod) &&
type.Methods.Any(IsOnExitMethod) &&
type.Methods.Any(IsOnExceptionMethod) &&
type.Methods.Any(IsOnTaskContinuationMethod);
}
示例10: InjectField
void InjectField(TypeDefinition type, FieldDefinition fieldDefinition)
{
var staticConstructor = type.GetStaticConstructor();
staticConstructor.Body.SimplifyMacros();
var genericInstanceMethod = new GenericInstanceMethod(forContextDefinition);
if (type.IsCompilerGenerated() && type.IsNested)
{
genericInstanceMethod.GenericArguments.Add(type.DeclaringType.GetGeneric());
}
else
{
genericInstanceMethod.GenericArguments.Add(type.GetGeneric());
}
var instructions = staticConstructor.Body.Instructions;
type.Fields.Add(fieldDefinition);
var returns = instructions.Where(_ => _.OpCode == OpCodes.Ret)
.ToList();
var ilProcessor = staticConstructor.Body.GetILProcessor();
foreach (var returnInstruction in returns)
{
var newReturn = Instruction.Create(OpCodes.Ret);
ilProcessor.InsertAfter(returnInstruction, newReturn);
ilProcessor.InsertBefore(newReturn, Instruction.Create(OpCodes.Call, genericInstanceMethod));
ilProcessor.InsertBefore(newReturn, Instruction.Create(OpCodes.Stsfld, fieldDefinition.GetGeneric()));
returnInstruction.OpCode = OpCodes.Nop;
}
staticConstructor.Body.OptimizeMacros();
}
示例11: InjectMethod
MethodDefinition InjectMethod(TypeDefinition targetType, string eventInvokerName, FieldReference propertyChangingField)
{
var method = new MethodDefinition(eventInvokerName, GetMethodAttributes(targetType), ModuleDefinition.TypeSystem.Void);
method.Parameters.Add(new ParameterDefinition("propertyName", ParameterAttributes.None, ModuleDefinition.TypeSystem.String));
var handlerVariable = new VariableDefinition(PropChangingHandlerReference);
method.Body.Variables.Add(handlerVariable);
var boolVariable = new VariableDefinition(ModuleDefinition.TypeSystem.Boolean);
method.Body.Variables.Add(boolVariable);
var instructions = method.Body.Instructions;
var last = Instruction.Create(OpCodes.Ret);
instructions.Add(Instruction.Create(OpCodes.Ldarg_0));
instructions.Add(Instruction.Create(OpCodes.Ldfld, propertyChangingField));
instructions.Add(Instruction.Create(OpCodes.Stloc_0));
instructions.Add(Instruction.Create(OpCodes.Ldloc_0));
instructions.Add(Instruction.Create(OpCodes.Ldnull));
instructions.Add(Instruction.Create(OpCodes.Ceq));
instructions.Add(Instruction.Create(OpCodes.Stloc_1));
instructions.Add(Instruction.Create(OpCodes.Ldloc_1));
instructions.Add(Instruction.Create(OpCodes.Brtrue_S, last));
instructions.Add(Instruction.Create(OpCodes.Ldloc_0));
instructions.Add(Instruction.Create(OpCodes.Ldarg_0));
instructions.Add(Instruction.Create(OpCodes.Ldarg_1));
instructions.Add(Instruction.Create(OpCodes.Newobj, ComponentModelPropertyChangingEventConstructorReference));
instructions.Add(Instruction.Create(OpCodes.Callvirt, ComponentModelPropertyChangingEventHandlerInvokeReference));
instructions.Add(last);
method.Body.InitLocals = true;
targetType.Methods.Add(method);
return method;
}
示例12: InjectInterceptedMethod
MethodDefinition InjectInterceptedMethod(TypeDefinition targetType, MethodDefinition innerOnPropertyChanging)
{
var delegateHolderInjector = new DelegateHolderInjector
{
TargetTypeDefinition=targetType,
OnPropertyChangingMethodReference = innerOnPropertyChanging,
ModuleWeaver = this
};
delegateHolderInjector.InjectDelegateHolder();
var method = new MethodDefinition(EventInvokerNames.First(), GetMethodAttributes(targetType), ModuleDefinition.TypeSystem.Void);
var propertyName = new ParameterDefinition("propertyName", ParameterAttributes.None, ModuleDefinition.TypeSystem.String);
method.Parameters.Add(propertyName);
if (InterceptorType == InvokerTypes.Before)
{
var before = new ParameterDefinition("before", ParameterAttributes.None, ModuleDefinition.TypeSystem.Object);
method.Parameters.Add(before);
}
var action = new VariableDefinition("firePropertyChanging", ActionTypeReference);
method.Body.Variables.Add(action);
var variableDefinition = new VariableDefinition("delegateHolder", delegateHolderInjector.TypeDefinition);
method.Body.Variables.Add(variableDefinition);
var instructions = method.Body.Instructions;
var last = Instruction.Create(OpCodes.Ret);
instructions.Add(Instruction.Create(OpCodes.Newobj, delegateHolderInjector.ConstructorDefinition));
instructions.Add(Instruction.Create(OpCodes.Stloc_1));
instructions.Add(Instruction.Create(OpCodes.Ldloc_1));
instructions.Add(Instruction.Create(OpCodes.Ldarg_1));
instructions.Add(Instruction.Create(OpCodes.Stfld, delegateHolderInjector.PropertyName));
instructions.Add(Instruction.Create(OpCodes.Ldloc_1));
instructions.Add(Instruction.Create(OpCodes.Ldarg_0));
instructions.Add(Instruction.Create(OpCodes.Stfld, delegateHolderInjector.Target));
instructions.Add(Instruction.Create(OpCodes.Ldloc_1));
instructions.Add(Instruction.Create(OpCodes.Ldftn, delegateHolderInjector.MethodDefinition));
instructions.Add(Instruction.Create(OpCodes.Newobj, ActionConstructorReference));
instructions.Add(Instruction.Create(OpCodes.Stloc_0));
instructions.Add(Instruction.Create(OpCodes.Ldarg_0));
instructions.Add(Instruction.Create(OpCodes.Ldloc_0));
instructions.Add(Instruction.Create(OpCodes.Ldloc_1));
instructions.Add(Instruction.Create(OpCodes.Ldfld, delegateHolderInjector.PropertyName));
if (InterceptorType == InvokerTypes.Before)
{
instructions.Add(Instruction.Create(OpCodes.Ldarg_2));
instructions.Add(Instruction.Create(OpCodes.Call, InterceptMethod));
}
else
{
instructions.Add(Instruction.Create(OpCodes.Call, InterceptMethod));
}
instructions.Add(last);
method.Body.InitLocals = true;
targetType.Methods.Add(method);
return method;
}
示例13: ShouldInclude
private static bool ShouldInclude(TypeDefinition type)
{
if (!type.IsClass)
{
return false;
}
if (type.IsSealed)
{
return false;
}
if (type.HasInterfaces)
{
return false;
}
if (type.IsNotPublic)
{
return false;
}
if (IsContainer(type))
{
return false;
}
return true;
}
示例14: GetBaseType
internal static TypeDefinition GetBaseType(TypeDefinition child)
{
if (child.BaseType == null)
return null;
return child.BaseType.Resolve ();
}
示例15: RecursiveFindEventInvoker
public EventInvokerMethod RecursiveFindEventInvoker(TypeDefinition typeDefinition)
{
var typeDefinitions = new Stack<TypeDefinition>();
MethodDefinition methodDefinition;
var currentTypeDefinition = typeDefinition;
do
{
typeDefinitions.Push(currentTypeDefinition);
if (FindEventInvokerMethodDefinition(currentTypeDefinition, out methodDefinition))
{
break;
}
var baseType = currentTypeDefinition.BaseType;
if (baseType == null || baseType.FullName == "System.Object")
{
return null;
}
currentTypeDefinition = Resolve(baseType);
} while (true);
return new EventInvokerMethod
{
MethodReference = GetMethodReference(typeDefinitions, methodDefinition),
IsVisibleFromChildren = IsVisibleFromChildren(methodDefinition),
InvokerType = ClassifyInvokerMethod(methodDefinition),
};
}