📄 membercreationservice.cs
字号:
CreateStaticFieldForDependencyProperty(className, eventName, eventType, fieldName, false, true);
eventInfo.UserData["_vsEventHandlerTypeKey"] = fieldName;
}
string nsName = null;
Helpers.GetNamespaceAndClassName(className, out nsName, out className);
CodeTypeDeclaration typeDeclaration = GetCodeTypeDeclFromCodeCompileUnit(nsName, className);
int index = 0;
foreach (CodeTypeMember member in typeDeclaration.Members)
{
if (member is CodeMemberEvent)
index++;
else
break;
}
ITypeProvider typeProvider = this.ServiceProvider.GetService(typeof(ITypeProvider)) as ITypeProvider;
if (typeProvider == null)
throw new InvalidOperationException(typeof(ITypeProvider).FullName);
typeDeclaration.Members.Insert(index, eventInfo);
((TypeProvider)typeProvider).RefreshCodeCompileUnit(this.loader.CodeBesideCCU, new EventHandler(RefreshCCU));
}
}
public void UpdateEvent(string className, string oldEventName, Type oldEventType, string newEventName, Type newEventType, AttributeInfo[] attributes, bool emitDependencyProperty, bool isMetaProperty)
{
throw new NotImplementedException();
}
public void RemoveEvent(string className, string eventName, Type eventType)
{
throw new NotImplementedException();
}
public void ShowCode(Activity contextActivity, string methodName, Type delegateType)
{
throw new NotImplementedException();
}
public void ShowCode()
{
throw new NotImplementedException();
}
public void ShowCode(string fileName)
{
throw new NotImplementedException();
}
public void UpdateBaseType(string className, Type baseType)
{
throw new NotImplementedException();
}
public void UpdateTypeName(string oldClassName, string newClassName)
{
throw new NotImplementedException();
}
#endregion
#region Internal Methods
internal static string FormatType(Type type)
{
string typeName = string.Empty;
if (type.IsArray)
{
typeName = FormatType(type.GetElementType());
typeName += '[';
typeName += new string(',', type.GetArrayRank() - 1);
typeName += ']';
}
else
{
typeName = type.FullName;
int indexOfSpecialChar = typeName.IndexOf('`');
if (indexOfSpecialChar != -1)
typeName = typeName.Substring(0, indexOfSpecialChar);
typeName = typeName.Replace('+', '.');
if (type.ContainsGenericParameters || type.IsGenericType)
{
Type[] genericArguments = type.GetGenericArguments();
typeName += '<';
bool first = true;
foreach (Type genericArgument in genericArguments)
{
if (!first)
typeName += ", ";
else
{
first = false;
}
typeName += FormatType(genericArgument);
}
typeName += '>';
}
}
return typeName;
}
#endregion
#region Private Methods
private void RefreshCCU(object sender, EventArgs e)
{
// we dont want to do anything here
}
#endregion
#region Helper Properties
private WorkflowLoader Loader
{
get
{
return this.loader;
}
}
private IServiceProvider ServiceProvider
{
get
{
return this.serviceProvider;
}
}
private CodeDomProvider CodeProvider
{
get
{
return this.provider;
}
}
private Activity RootActivity
{
get
{
IDesignerHost host = this.ServiceProvider.GetService(typeof(IDesignerHost)) as IDesignerHost;
if (host == null)
throw new InvalidOperationException(typeof(IDesignerHost).FullName);
return host.RootComponent as Activity;
}
}
#endregion
#region Helper Methods
private string GeneratePropertyAssociatedFieldName(string className, string propertyName, Type propertyType, out bool exisitingField)
{
exisitingField = false;
IIdentifierCreationService identCreationService = this.ServiceProvider.GetService(typeof(IIdentifierCreationService)) as IIdentifierCreationService;
if (identCreationService == null)
throw new InvalidOperationException();
string baseFieldName = "_";
if (propertyName.StartsWith("@"))
baseFieldName += propertyName.Substring(1);
else
baseFieldName += propertyName;
bool validIdent = false;
int identCount = 1;
string fieldName = String.Empty;
while (!validIdent && identCount < Int32.MaxValue)
{
fieldName = baseFieldName + System.Convert.ToString(identCount);
try
{
identCreationService.ValidateIdentifier(RootActivity, fieldName);
validIdent = true;
//See if the field already exists, and if it's the same type
if (DoesFieldExist(className, fieldName, propertyType))
exisitingField = true;
else if (DoesFieldExist(className, fieldName))
validIdent = false;
}
catch
{
validIdent = false;
}
identCount += 1;
}
return fieldName;
}
private bool DoesFieldExist(string className, string fieldName)
{
return DoesFieldExist(className, fieldName, null);
}
private bool DoesFieldExist(string className, string fieldName, Type fieldType)
{
if (className == null || className.Length == 0)
return false;
ITypeProvider typeProvider = (ITypeProvider)this.ServiceProvider.GetService(typeof(ITypeProvider));
if (typeProvider == null)
throw new Exception("Error_MissingTypeProvider");
Type typeDeclaration = typeProvider.GetType(className, true);
foreach (FieldInfo member in typeDeclaration.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static))
{
if (fieldType == null || member.FieldType == fieldType)
{
if (member.Name == fieldName)
return true;
}
}
return false;
}
private bool DoesPropertyExist(string className, string propertyName, Type propertyType)
{
if (string.IsNullOrEmpty(className) || string.IsNullOrEmpty(propertyName) || propertyType == null)
return false;
ITypeProvider typeProvider = (ITypeProvider)this.ServiceProvider.GetService(typeof(ITypeProvider));
if (typeProvider == null)
throw new Exception("Error_MissingTypeProvider");
Type typeDeclaration = typeProvider.GetType(className, true);
foreach (PropertyInfo property in typeDeclaration.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
{
if (property.Name == propertyName && property.PropertyType == propertyType)
return true;
}
return false;
}
private bool DoesEventExist(string className, string eventName, Type eventHandlerType)
{
if (string.IsNullOrEmpty(className) || string.IsNullOrEmpty(eventName) || eventHandlerType == null)
return false;
ITypeProvider typeProvider = (ITypeProvider)this.ServiceProvider.GetService(typeof(ITypeProvider));
if (typeProvider == null)
throw new Exception("Error_MissingTypeProvider");
Type typeDeclaration = typeProvider.GetType(className, true);
foreach (EventInfo eventInfo in typeDeclaration.GetEvents(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
{
if (eventInfo.Name == eventName && eventInfo.EventHandlerType == eventHandlerType)
return true;
}
return false;
}
private CodeTypeReference GetCodeTypeReference(string className, Type type)
{
CodeTypeReference codeTypeReference = new CodeTypeReference();
codeTypeReference.ArrayRank = 0;
codeTypeReference.ArrayElementType = null;
codeTypeReference.BaseType = type.FullName;
return codeTypeReference;
}
private CodeTypeDeclaration GetCodeTypeDeclFromCodeCompileUnit(string nsName, string className)
{
CodeTypeDeclaration typeDeclaration = null;
foreach (CodeNamespace ns in this.loader.CodeBesideCCU.Namespaces)
{
if (ns.Name == nsName)
{
foreach (CodeTypeDeclaration decl in ns.Types)
{
if (decl.Name == className)
{
typeDeclaration = decl;
break;
}
}
}
}
return typeDeclaration;
}
private void CreateStaticFieldForDependencyProperty(string className, string propertyName, Type propertyType, string fieldName, bool isMetaProperty, bool isEvent)
{
//Emit the DependencyProperty.Register statement
CodeSnippetExpression initExpression = (CodeSnippetExpression)CreateStaticFieldInitExpression(className, propertyName, propertyType.FullName, TypeProvider.IsAssignable(typeof(Delegate), propertyType), isMetaProperty, isEvent);
CreateField(className, fieldName, typeof(DependencyProperty), null, MemberAttributes.Public | MemberAttributes.Static, initExpression, true);
}
private CodeExpression CreateStaticFieldInitExpression(string className, string propertyName, string propTypeName, bool isDelegateType, bool isMetaProperty, bool isEvent)
{
CodeSnippetExpression initExpression = new CodeSnippetExpression();
const string DependencyPropertyInit_CS = "DependencyProperty.Register(\"{0}\", typeof({1}), typeof({2}){3})";
const string DependencyPropertyOption = ", new PropertyMetadata({0})";
string metaOptions = string.Empty;
if (isMetaProperty)
metaOptions = "DependencyPropertyOptions.Metadata";
if (!isEvent && isDelegateType)
{
if (!string.IsNullOrEmpty(metaOptions))
{
metaOptions += " | ";
metaOptions += "DependencyPropertyOptions.DelegateProperty";
}
else
metaOptions = "DependencyPropertyOptions.DelegateProperty";
}
if (!string.IsNullOrEmpty(metaOptions))
metaOptions = string.Format(DependencyPropertyOption, metaOptions);
initExpression.Value = String.Format(DependencyPropertyInit_CS, propertyName, propTypeName, className, metaOptions);
return initExpression;
}
#endregion
}
#endregion
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -