📄 eventbindingservice.cs
字号:
//---------------------------------------------------------------------
// This file is part of the WindowsWorkflow.NET web site samples.
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
// This source code is intended only as a supplement to Microsoft
// Development Tools and/or on-line documentation. See these other
// materials for detailed information regarding Microsoft code samples.
//
// THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//---------------------------------------------------------------------
namespace WorkflowDesignerControl
{
#region Using StateMents
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel.Design;
using System.ComponentModel;
using System.Globalization;
using System.Collections;
using System.Workflow.ComponentModel.Compiler;
using System.CodeDom;
using System.Reflection;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Serialization;
#endregion
#region Class EventBindingService
internal class EventBindingService : IEventBindingService
{
private WorkflowLoader loader;
private IServiceProvider serviceProvider;
public EventBindingService(IServiceProvider provider, WorkflowLoader loader)
{
this.loader = loader;
this.serviceProvider = provider;
}
public string CreateUniqueMethodName(IComponent component, EventDescriptor e)
{
return e.DisplayName;
}
public ICollection GetCompatibleMethods(EventDescriptor e)
{
return new ArrayList();
}
public EventDescriptor GetEvent(PropertyDescriptor property)
{
return (property is EventPropertyDescriptor) ? ((EventPropertyDescriptor)property).EventDescriptor : null;
}
public PropertyDescriptorCollection GetEventProperties(EventDescriptorCollection events)
{
return new PropertyDescriptorCollection(new PropertyDescriptor[] { }, true);
}
public PropertyDescriptor GetEventProperty(EventDescriptor e)
{
return new EventPropertyDescriptor(e, this, this.serviceProvider);
}
public bool ShowCode()
{
return false;
}
public bool ShowCode(int lineNumber)
{
return false;
}
public bool ShowCode(IComponent component, EventDescriptor e)
{
return false;
}
protected void UseMethod(IComponent component, EventDescriptor e, string methodName)
{
if (!String.IsNullOrEmpty(methodName))
{
Generate(component, e, methodName, true);
}
}
protected void FreeMethod(IComponent component, EventDescriptor e, string methodName)
{
if (!String.IsNullOrEmpty(methodName))
{
Generate(component, e, methodName, false);
}
}
/// <summary>
/// Generate the method and add to code compile unit
/// </summary>
/// <param name="component"></param>
/// <param name="e"></param>
/// <param name="methodName"></param>
/// <param name="addMethod"></param>
private void Generate(IComponent component, EventDescriptor e, string methodName, bool addMethod)
{
TypeProvider typeProvider = (TypeProvider)this.serviceProvider.GetService(typeof(ITypeProvider));
CodeCompileUnit ccu = this.loader.CodeBesideCCU;
Type delegateType = e.EventType;
MethodInfo invokeInfo = delegateType.GetMethod("Invoke");
Type returnType = invokeInfo.ReturnType;
MemberAttributes modifiers = MemberAttributes.Private;
PropertyDescriptor modifiersProp = TypeDescriptor.GetProperties(component)["DefaultEventModifiers"];
if (modifiersProp != null && modifiersProp.PropertyType == typeof(MemberAttributes))
{
modifiers = (MemberAttributes)modifiersProp.GetValue(component);
}
CodeParameterDeclarationExpressionCollection paramCollection = new CodeParameterDeclarationExpressionCollection();
foreach (ParameterInfo paramInfo in invokeInfo.GetParameters())
{
CodeParameterDeclarationExpression codeParamExpression = new CodeParameterDeclarationExpression();
codeParamExpression.Name = paramInfo.Name;
codeParamExpression.Type = new CodeTypeReference(paramInfo.ParameterType);
paramCollection.Add(codeParamExpression);
}
// create code member method
CodeMemberMethod method = new CodeMemberMethod();
method.Name = methodName;
method.Parameters.AddRange(paramCollection);
method.ReturnType = new CodeTypeReference(returnType);
method.Attributes = modifiers;
if (addMethod)
// The assumption here is that we have only one namespace and type being designed
ccu.Namespaces[0].Types[0].Members.Add(method);
else
ccu.Namespaces[0].Types[0].Members.Remove(method);
typeProvider.RefreshCodeCompileUnit(this.loader.CodeBesideCCU, new EventHandler(RefreshCCU));
}
private void RefreshCCU(object sender, EventArgs e)
{
//We dont want to do anything here
}
private class EventPropertyDescriptor : PropertyDescriptor
{
private EventDescriptor eventDescriptor;
private IServiceProvider serviceProvider;
private EventBindingService eventSvc;
private TypeConverter converter;
private bool useMethodCalled = false;
public EventDescriptor EventDescriptor
{
get
{
return this.eventDescriptor;
}
}
public EventPropertyDescriptor(EventDescriptor eventDesc, EventBindingService eventSvc, IServiceProvider serviceProvider)
: base(eventDesc, null)
{
this.eventDescriptor = eventDesc;
this.eventSvc = eventSvc;
this.serviceProvider = serviceProvider;
}
public override Type ComponentType
{
get
{
return this.eventDescriptor.ComponentType;
}
}
public override Type PropertyType
{
get
{
return this.eventDescriptor.EventType;
}
}
public override TypeConverter Converter
{
get
{
if (this.converter == null)
this.converter = new XomlEventConverter(this.eventDescriptor);
return this.converter;
}
}
public override bool IsReadOnly
{
get
{
return false;
}
}
public override bool CanResetValue(object component)
{
return false;
}
public override object GetValue(object component)
{
string value = null;
DependencyObject dependencyObject = component as DependencyObject;
if (dependencyObject != null)
{
Hashtable dynamicEvents = dependencyObject.GetValue(WorkflowMarkupSerializer.EventsProperty) as Hashtable;
if (dynamicEvents != null)
{
if (dynamicEvents.ContainsKey(this.eventDescriptor.Name))
value = dynamicEvents[this.eventDescriptor.Name] as string;
}
else
{
DependencyProperty dependencyEvent = DependencyProperty.FromName(this.eventDescriptor.Name, dependencyObject.GetType());
MethodInfo getInvocationListMethod = dependencyObject.GetType().GetMethod("System.Workflow.ComponentModel.IDependencyObjectAccessor.GetInvocationList", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
if (getInvocationListMethod != null && dependencyEvent != null && dependencyEvent.IsEvent)
{
MethodInfo boundGetInvocationListMethod = getInvocationListMethod.MakeGenericMethod(new Type[] { dependencyEvent.PropertyType });
if (boundGetInvocationListMethod != null)
{
Delegate[] delegates = boundGetInvocationListMethod.Invoke(dependencyObject, new object[] { dependencyEvent }) as Delegate[];
if (delegates != null && delegates.Length > 0 && delegates[0].Method != null)
value = delegates[0].Method.Name;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -