本文整理汇总了C#中System.Reflection.Emit.TypeBuilder.DefineEvent方法的典型用法代码示例。如果您正苦于以下问题:C# TypeBuilder.DefineEvent方法的具体用法?C# TypeBuilder.DefineEvent怎么用?C# TypeBuilder.DefineEvent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.Emit.TypeBuilder
的用法示例。
在下文中一共展示了TypeBuilder.DefineEvent方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Generate
public static void Generate(
TypeBuilder typeBuilder,
EventMetadata eventMetadata,
MethodBuilder addMethod,
MethodBuilder removeMethod,
MethodBuilder raiseMethod)
{
var @event = typeBuilder.DefineEvent(
eventMetadata.Name,
eventMetadata.EventAttributes,
eventMetadata.EventHandlerType);
if (addMethod != null)
{
@event.SetAddOnMethod(addMethod);
}
if (removeMethod != null)
{
@event.SetRemoveOnMethod(removeMethod);
}
if (raiseMethod != null)
{
@event.SetRaiseMethod(raiseMethod);
}
}
示例2: CreateProxyEventBuilder
private EventBuilder CreateProxyEventBuilder(TypeBuilder typeBuilder, EventInfo contractEvent)
{
var builder = typeBuilder.DefineEvent(contractEvent.Name, contractEvent.Attributes,
contractEvent.EventHandlerType);
return builder;
}
示例3: SetUp
protected void SetUp () {
AssemblyName assemblyName = new AssemblyName();
assemblyName.Name = GetType().FullName;
AssemblyBuilder assembly
= Thread.GetDomain().DefineDynamicAssembly(
assemblyName, AssemblyBuilderAccess.Run);
module = assembly.DefineDynamicModule("module1");
tb = module.DefineType("class1",
TypeAttributes.Public);
eb = tb.DefineEvent ("event1", EventAttributes.None, typeof (AnEvent));
mb =
tb.DefineMethod ("OnAnEvent",
MethodAttributes.Public, typeof (void),
new Type [] { typeof (AnEvent) });
ILGenerator ilgen = mb.GetILGenerator();
ilgen.Emit (OpCodes.Ret);
// These two are required
eb.SetAddOnMethod (mb);
eb.SetRemoveOnMethod (mb);
}
示例4: DefineEvent
public static void DefineEvent(TypeBuilder typeBuilder, EventInfo eventInfo,
MethodBuilder addMethodBuilder, MethodBuilder removeMethodBuilder)
{
String eventName = eventInfo.DeclaringType.FullName + "." + eventInfo.Name;
EventBuilder eventBuilder = typeBuilder.DefineEvent(eventName, EventAttributes.None, eventInfo.EventHandlerType);
eventBuilder.SetAddOnMethod(addMethodBuilder);
eventBuilder.SetRemoveOnMethod(removeMethodBuilder);
}
示例5: ImplementEvent
private EventBuilder ImplementEvent(TypeBuilder typeBuilder, EventInfo eventToImplement, Type targetInterface)
{
var eventBuilder = typeBuilder.DefineEvent(eventToImplement.Name, EventAttributes.None, eventToImplement.EventHandlerType);
var addMethodInfo = eventToImplement.GetAddMethod();
var removeMethodInfo = eventToImplement.GetRemoveMethod();
var addMethodBuilder = ImplementMethod(typeBuilder, addMethodInfo, targetInterface);
var removeMethodBuilder = ImplementMethod(typeBuilder, removeMethodInfo, targetInterface);
eventBuilder.SetAddOnMethod(addMethodBuilder);
eventBuilder.SetRemoveOnMethod(removeMethodBuilder);
return eventBuilder;
}
示例6: Build
public void Build(TypeBuilder builder, Type contractType)
{
builder.AddInterfaceImplementation(typeof(IAutoNotifyPropertyChanged));
FieldBuilder addPropertyChangedField = builder.DefineField("PropertyChanged", typeof (PropertyChangingEventHandler), FieldAttributes.Private);
MethodBuilder addMethod = DefineAddOnMethod(builder, addPropertyChangedField);
MethodBuilder removeMethod = DefineRemoveOnMethod(builder, addPropertyChangedField);
MethodBuilder notifyPropertyChangedMethod = DefineRaiseMethod(builder, addPropertyChangedField);
EventBuilder pcevent = builder.DefineEvent("PropertyChanged", EventAttributes.None, typeof(PropertyChangedEventHandler));
pcevent.SetRaiseMethod(notifyPropertyChangedMethod);
pcevent.SetAddOnMethod(addMethod);
pcevent.SetRemoveOnMethod(removeMethod);
}
示例7: DefineEvent
private EventBuilder DefineEvent(TypeBuilder typeBuilder, Cci.IEventDefinition eventDef)
{
EventAttributes attrs = (EventAttributes)Cci.MetadataWriter.GetEventFlags(eventDef);
Type type = ResolveType(eventDef.GetType(_context));
// Event, EventMap
var eventBuilder = typeBuilder.DefineEvent(eventDef.Name, attrs, type);
// MethodSemantics
foreach (var accessor in eventDef.Accessors)
{
var accessorDef = (Cci.IMethodDefinition)accessor.AsDefinition(_context);
var accessorBuilder = _methodBuilders[accessorDef];
if (accessor == eventDef.Adder)
{
eventBuilder.SetAddOnMethod(accessorBuilder);
}
else if (accessor == eventDef.Remover)
{
eventBuilder.SetRemoveOnMethod(accessorBuilder);
}
else if (accessor == eventDef.Caller)
{
eventBuilder.SetRaiseMethod(accessorBuilder);
}
else
{
eventBuilder.AddOtherMethod(accessorBuilder);
}
}
return eventBuilder;
}
示例8: Define
public static void Define(TypeBuilder typeB, Interface iface)
{
foreach (Method declMethod in iface.Methods)
DefineMethod (typeB, declMethod.Name, ifaceMethAttr, declMethod.Arguments, false);
if (iface.Properties != null)
foreach (NDesk.DBus.Introspection.Property prop in iface.Properties) {
Type propType = new Signature (prop.Type).ToType ();
PropertyBuilder prop_builder = typeB.DefineProperty (prop.Name, PropertyAttributes.None, propType, Type.EmptyTypes);
if (prop.Access == propertyAccess.read || prop.Access == propertyAccess.readwrite)
prop_builder.SetGetMethod (typeB.DefineMethod ("get_" + prop.Name, ifaceMethAttr | MethodAttributes.SpecialName, propType, Type.EmptyTypes));
if (prop.Access == propertyAccess.write || prop.Access == propertyAccess.readwrite)
prop_builder.SetSetMethod (typeB.DefineMethod ("set_" + prop.Name, ifaceMethAttr | MethodAttributes.SpecialName, null, new Type[] {propType}));
}
if (iface.Signals != null)
foreach (NDesk.DBus.Introspection.Signal signal in iface.Signals) {
Type eventType = DefineHandler (modBdef, signal);
EventBuilder event_builder = typeB.DefineEvent (signal.Name, EventAttributes.None, eventType);
event_builder.SetAddOnMethod (typeB.DefineMethod ("add_" + signal.Name, ifaceMethAttr | MethodAttributes.SpecialName, null, new Type[] {eventType}));
event_builder.SetRemoveOnMethod (typeB.DefineMethod ("remove_" + signal.Name, ifaceMethAttr | MethodAttributes.SpecialName, null, new Type[] {eventType}));
}
//apply InterfaceAttribute
ConstructorInfo interfaceAttributeCtor = typeof (InterfaceAttribute).GetConstructor(new Type[] {typeof (string)});
CustomAttributeBuilder cab = new CustomAttributeBuilder (interfaceAttributeCtor, new object[] {iface.Name});
typeB.SetCustomAttribute (cab);
}
示例9: MyEventBuilder
public MyEventBuilder (Event ev, TypeBuilder type_builder, string name, EventAttributes event_attr, Type event_type)
{
MyBuilder = type_builder.DefineEvent (name, event_attr, event_type);
// And now store the values in our own fields.
declaring_type = type_builder;
reflected_type = type_builder;
attributes = event_attr;
this.name = name;
my_event = ev;
this.event_type = event_type;
}
示例10: MakeEvent
private static void MakeEvent(ModuleBuilder builder, EventInfo info, TypeBuilder typeBuilder, Type contextType, bool defaultImp)
{
var tName = info.Name;
var tAddMethod = info.GetAddMethod();
var tRemoveMethod = info.GetRemoveMethod();
var tReturnType = info.EventHandlerType;
var tCallSiteInvokeName = string.Format("Impromptu_Callsite_{1}_{0}", Guid.NewGuid().ToString("N"), tName);
var tCStp = DefineBuilderForCallSite(builder, tCallSiteInvokeName);
var tInvokeIsEvent = "Invoke_IsEvent";
var tInvokeIseventFuncType = tCStp.DefineCallsiteField(tInvokeIsEvent, typeof (bool));
var tInvokeAddAssign = "Invoke_AddAssign";
var tInvokeAddAssignFuncType = tCStp.DefineCallsiteField(tInvokeAddAssign, typeof (object), tReturnType);
var tInvokeSubtractAssign = "Invoke_SubtractAssign";
var tInvokeSubtractAssignFuncType = tCStp.DefineCallsiteField(tInvokeSubtractAssign, typeof (object), tReturnType);
var tAddParamTypes = tRemoveMethod.GetParameters().Select(it => it.ParameterType).ToArray();
var tInvokeAdd = "Invoke_Add";
var tInvokeAddFuncType = tCStp.DefineCallsiteField(tInvokeAdd, typeof (object), tAddParamTypes);
var tRemoveParamTypes = tRemoveMethod.GetParameters().Select(it => it.ParameterType).ToArray();
var tInvokeRemove = "Invoke_Remove";
var tInvokeRemoveFuncType = tCStp.DefineCallsiteField(tInvokeRemove, typeof (object), tRemoveParamTypes);
var tInvokeGet = "Invoke_Get";
var tInvokeGetFuncType = tCStp.DefineCallsiteField(tInvokeGet, typeof (object));
var tInvokeSet = "Invoke_Set";
var tInvokeSetFuncType = tCStp.DefineCallsiteField(tInvokeSet, typeof (object), typeof (object));
var tCallSite = tCStp.CreateType();
var tMp = typeBuilder.DefineEvent(tName, EventAttributes.None, tReturnType);
//AddMethod
var tPublicPrivate = MethodAttributes.Public;
var tAddPrefixName = tAddMethod.Name;
if (!defaultImp) {
tAddPrefixName = String.Format("{0}.{1}", info.DeclaringType.FullName, tAddPrefixName);
tPublicPrivate = MethodAttributes.Private;
}
var tAddBuilder = typeBuilder.DefineMethod(tAddPrefixName,
tPublicPrivate | MethodAttributes.SpecialName | MethodAttributes.HideBySig | MethodAttributes.Virtual | MethodAttributes.Final | MethodAttributes.NewSlot,
typeof (void),
tAddParamTypes);
if (!defaultImp) {
typeBuilder.DefineMethodOverride(tAddBuilder, info.GetAddMethod());
}
foreach (var tParam in tAddMethod.GetParameters()) {
tAddBuilder.DefineParameter(tParam.Position + 1, AttributesForParam(tParam), tParam.Name);
}
var tIlGen = tAddBuilder.GetILGenerator();
var tIsEventField = tCallSite.GetFieldEvenIfGeneric(tInvokeIsEvent);
using (tIlGen.EmitBranchTrue(gen => gen.Emit(OpCodes.Ldsfld, tIsEventField))) {
tIlGen.EmitDynamicIsEventBinder(CSharpBinderFlags.None, tName, contextType);
tIlGen.EmitCallsiteCreate(tInvokeIseventFuncType);
tIlGen.Emit(OpCodes.Stsfld, tIsEventField);
}
var tSetField = tCallSite.GetFieldEvenIfGeneric(tInvokeSet);
var tGetField = tCallSite.GetFieldEvenIfGeneric(tInvokeGet);
using (tIlGen.EmitBranchTrue(
load => load.EmitInvocation(
target => target.EmitInvocation(
t => t.Emit(OpCodes.Ldsfld, tIsEventField),
i => i.Emit(OpCodes.Ldfld, tIsEventField.FieldType.GetFieldEvenIfGeneric("Target"))
),
invoke => invoke.EmitCallInvokeFunc(tInvokeIseventFuncType),
param => param.Emit(OpCodes.Ldsfld, tIsEventField),
param => param.EmitInvocation(
t => t.Emit(OpCodes.Ldarg_0),
i => i.Emit(OpCodes.Call, typeof (ActLikeProxy).GetProperty("Original").GetGetMethod())
)
)
)
) //if IsEvent Not True
{
using (tIlGen.EmitBranchTrue(gen => gen.Emit(OpCodes.Ldsfld, tSetField))) {
tIlGen.EmitDynamicSetBinderDynamicParams(CSharpBinderFlags.ValueFromCompoundAssignment, tName, contextType, typeof (Object));
tIlGen.EmitCallsiteCreate(tInvokeSetFuncType);
tIlGen.Emit(OpCodes.Stsfld, tSetField);
}
var tAddAssigneField = tCallSite.GetFieldEvenIfGeneric(tInvokeAddAssign);
using (tIlGen.EmitBranchTrue(gen => gen.Emit(OpCodes.Ldsfld, tAddAssigneField))) {
tIlGen.EmitDynamicBinaryOpBinder(CSharpBinderFlags.None, ExpressionType.AddAssign, contextType, tReturnType);
tIlGen.EmitCallsiteCreate(tInvokeAddAssignFuncType);
//.........这里部分代码省略.........
示例11: AddEvent
private static FieldBuilder AddEvent(TypeBuilder typeBuilder, string eventName, Type eventHandlerType)
{
EventBuilder eventBuilder = typeBuilder.DefineEvent(eventName, EventAttributes.None, eventHandlerType);
FieldBuilder backingField = typeBuilder.DefineField(eventName, eventHandlerType, FieldAttributes.Private);
MethodBuilder addMethod = typeBuilder.DefineMethod("add_" + eventName,
MethodAttributes.Public | MethodAttributes.HideBySig |
MethodAttributes.SpecialName,
CallingConventions.Standard, typeof(void), new [] { eventHandlerType });
ILGenerator generator = addMethod.GetILGenerator();
generator.DeclareLocal(eventHandlerType);
generator.DeclareLocal(eventHandlerType);
generator.DeclareLocal(eventHandlerType);
generator.DeclareLocal(typeof(bool));
generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Ldfld, backingField);
generator.Emit(OpCodes.Stloc_0);
Label loop = generator.DefineLabel();
generator.MarkLabel(loop);
generator.Emit(OpCodes.Ldloc_0);
generator.Emit(OpCodes.Stloc_1);
generator.Emit(OpCodes.Ldloc_1);
generator.Emit(OpCodes.Ldarg_1);
generator.Emit(OpCodes.Call, typeof(Delegate).GetMethod("Combine", new []{typeof(Delegate), typeof(Delegate)}));
generator.Emit(OpCodes.Castclass, eventHandlerType);
generator.Emit(OpCodes.Stloc_2);
generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Ldflda, backingField);
generator.Emit(OpCodes.Ldloc_2);
generator.Emit(OpCodes.Ldloc_1);
MethodInfo m = GetGenericMethod(typeof(Interlocked), "CompareExchange", new [] { eventHandlerType }, new [] { eventHandlerType, eventHandlerType, eventHandlerType });
generator.Emit(OpCodes.Call, m);
generator.Emit(OpCodes.Stloc_0);
generator.Emit(OpCodes.Ldloc_0);
generator.Emit(OpCodes.Ldloc_1);
generator.Emit(OpCodes.Ceq);
generator.Emit(OpCodes.Ldc_I4_0);
generator.Emit(OpCodes.Ceq);
generator.Emit(OpCodes.Stloc_3);
generator.Emit(OpCodes.Ldloc_3);
generator.Emit(OpCodes.Brtrue_S, loop);
generator.Emit(OpCodes.Ldstr, "ADD...");
EmitConsoleWriteLine(generator);
generator.Emit(OpCodes.Ret);
MethodBuilder removeMethod = typeBuilder.DefineMethod("remove_" + eventName,
MethodAttributes.Public | MethodAttributes.HideBySig |
MethodAttributes.SpecialName,
CallingConventions.Standard, typeof(void), new[] { eventHandlerType });
generator = removeMethod.GetILGenerator();
generator.Emit(OpCodes.Ldstr, "REMOVE...");
generator.Emit(OpCodes.Ret);
eventBuilder.SetAddOnMethod(addMethod);
eventBuilder.SetRemoveOnMethod(removeMethod);
return backingField;
}
示例12: CreatePropertyChangedEvent
/************************************************************************/
/************************************************************************/
private static FieldBuilder CreatePropertyChangedEvent(TypeBuilder typeBuilder)
{
// public event PropertyChangedEventHandler PropertyChanged;
FieldBuilder eventField = typeBuilder.DefineField("PropertyChanged", typeof(PropertyChangedEventHandler), FieldAttributes.Private);
EventBuilder eventBuilder = typeBuilder.DefineEvent("PropertyChanged", EventAttributes.None, typeof(PropertyChangedEventHandler));
eventBuilder.SetAddOnMethod(CreateAddRemoveMethod(typeBuilder, eventField, true));
eventBuilder.SetRemoveOnMethod(CreateAddRemoveMethod(typeBuilder, eventField, false));
return eventField;
}
示例13: Define
public static void Define(TypeBuilder typeB, Interface iface)
{
foreach (Method declMethod in iface.Methods) {
//MethodBuilder method_builder = typeB.DefineMethod (declMethod.Name, MethodAttributes.Public | MethodAttributes.Virtual, declMethod.ReturnType, Mapper.GetTypes (ArgDirection.In, declMethod.GetParameters ()));
List<Type> parms = new List<Type> ();
if (declMethod.Arguments != null)
foreach (Argument arg in declMethod.Arguments) {
if (arg.Direction == [email protected])
parms.Add (new Signature (arg.Type).ToType ());
//if (arg.Direction == [email protected])
// parms.Add (new Signature (arg.Type).ToType ().MakeByRefType ());
}
Signature outSig = Signature.Empty;
//this just takes the last out arg and uses is as the return type
if (declMethod.Arguments != null)
foreach (Argument arg in declMethod.Arguments)
if (arg.Direction == [email protected])
outSig = new Signature (arg.Type);
Type retType = outSig == Signature.Empty ? typeof (void) : outSig.ToType ();
MethodBuilder method_builder = typeB.DefineMethod (declMethod.Name, ifaceMethAttr, retType, parms.ToArray ());
//define the parameter attributes and names
if (declMethod.Arguments != null) {
int argNum = 0;
foreach (Argument arg in declMethod.Arguments) {
if (arg.Direction == [email protected])
method_builder.DefineParameter (++argNum, ParameterAttributes.In, arg.Name);
//if (arg.Direction == [email protected])
// method_builder.DefineParameter (++argNum, ParameterAttributes.Out, arg.Name);
}
}
}
if (iface.Properties != null)
foreach (NDesk.DBus.Introspection.Property prop in iface.Properties) {
Type propType = new Signature (prop.Type).ToType ();
PropertyBuilder prop_builder = typeB.DefineProperty (prop.Name, PropertyAttributes.None, propType, Type.EmptyTypes);
if (prop.Access == propertyAccess.read || prop.Access == propertyAccess.readwrite)
prop_builder.SetGetMethod (typeB.DefineMethod ("get_" + prop.Name, ifaceMethAttr | MethodAttributes.SpecialName, propType, Type.EmptyTypes));
if (prop.Access == propertyAccess.write || prop.Access == propertyAccess.readwrite)
prop_builder.SetSetMethod (typeB.DefineMethod ("set_" + prop.Name, ifaceMethAttr | MethodAttributes.SpecialName, null, new Type[] {propType}));
}
if (iface.Signals != null)
foreach (NDesk.DBus.Introspection.Signal signal in iface.Signals) {
//Type eventType = typeof (EventHandler);
Type eventType = typeof (VoidHandler);
EventBuilder event_builder = typeB.DefineEvent (signal.Name, EventAttributes.None, eventType);
event_builder.SetAddOnMethod (typeB.DefineMethod ("add_" + signal.Name, ifaceMethAttr | MethodAttributes.SpecialName, null, new Type[] {eventType}));
event_builder.SetRemoveOnMethod (typeB.DefineMethod ("remove_" + signal.Name, ifaceMethAttr | MethodAttributes.SpecialName, null, new Type[] {eventType}));
}
//apply InterfaceAttribute
ConstructorInfo interfaceAttributeCtor = typeof (InterfaceAttribute).GetConstructor(new Type[] {typeof (string)});
CustomAttributeBuilder cab = new CustomAttributeBuilder (interfaceAttributeCtor, new object[] {iface.Name});
typeB.SetCustomAttribute (cab);
}
示例14: GenerateEvents
private void GenerateEvents(List<string> usedNames, Type interfaceType, TypeBuilder tb, List<MethodInfo> propAccessors)
{
foreach (EventInfo eventInfo in interfaceType.GetEvents(BindingFlags.Instance | BindingFlags.Public))
{
if (usedNames.Contains(eventInfo.Name))
{
throw new NotSupportedException(string.Format("Error in interface {1}! Event name '{0}' already used in other child interface!", eventInfo.Name, interfaceType.Name)); //LOCSTR
}
else
{
usedNames.Add(eventInfo.Name);
}
EventBuilder eb = tb.DefineEvent(eventInfo.Name, eventInfo.Attributes, eventInfo.EventHandlerType);
FieldBuilder ef = tb.DefineField(string.Concat("_", eventInfo.Name), eventInfo.EventHandlerType, FieldAttributes.Private);
//add
{
MethodInfo addMethodInfo = eventInfo.GetAddMethod();
propAccessors.Add(addMethodInfo);
MethodBuilder getMb = tb.DefineMethod(addMethodInfo.Name, MethodAttributes.Public | MethodAttributes.Virtual, typeof(void), new Type[] { eventInfo.EventHandlerType });
ILGenerator ilGenerator = getMb.GetILGenerator();
EmitEventAdd(ilGenerator, eventInfo, ef);
tb.DefineMethodOverride(getMb, addMethodInfo);
}
//remove
{
MethodInfo removeMethodInfo = eventInfo.GetRemoveMethod();
propAccessors.Add(removeMethodInfo);
MethodBuilder getMb = tb.DefineMethod(removeMethodInfo.Name, MethodAttributes.Public | MethodAttributes.Virtual, typeof(void), new Type[] { eventInfo.EventHandlerType });
ILGenerator ilGenerator = getMb.GetILGenerator();
EmitEventRemove(ilGenerator, eventInfo, ef);
tb.DefineMethodOverride(getMb, removeMethodInfo);
}
}
}
示例15: CreateEvent
private static EventBuilder CreateEvent(TypeBuilder dest, EventInfo ev)
{
MethodBuilder add = null, remove = null;
var mi = ev.GetAddMethod();
if (mi != null)
{
add = CreateMethodCopy(dest, mi);
}
mi = ev.GetRemoveMethod();
if (mi != null)
{
remove = CreateMethodCopy(dest, mi);
}
var evt = dest.DefineEvent(ev.Name, ev.Attributes, ev.EventHandlerType);
if (add != null)
{
evt.SetAddOnMethod(add);
}
if (remove != null)
{
evt.SetRemoveOnMethod(remove);
}
return evt;
}