static void CacheLoadedAssembly(Assembly assembly) { // this VBImportReference is only used as an entry to the xmlnsMappings cache // and is never meant to be Xaml serialized. // those VBImportReferences that are to be Xaml serialized are created by Clone() method. XmlnsDefinitionAttribute[] attributes = (XmlnsDefinitionAttribute[])assembly.GetCustomAttributes(typeof(XmlnsDefinitionAttribute), false); string assemblyName = assembly.FullName; XmlnsMapping mapping; for (int i = 0; i < attributes.Length; ++i) { XNamespace xmlns = XNamespace.Get(attributes[i].XmlNamespace); if (!xmlnsMappings.TryGetValue(xmlns, out mapping)) { mapping.ImportReferences = new HashSet <VisualBasicImportReference>(); xmlnsMappings[xmlns] = mapping; } VisualBasicImportReference newImportReference = new VisualBasicImportReference { Assembly = assemblyName, Import = attributes[i].ClrNamespace, Xmlns = xmlns, }; // early binding the assembly // this leads to the short-cut, skipping the normal assembly resolution routine newImportReference.EarlyBoundAssembly = assembly; mapping.ImportReferences.Add(newImportReference); } }
/// <summary> /// Get Visual Basic settings containing importings of assemblies and namespaces. /// </summary> public VisualBasicSettings GetVisualBasicSettings() { VisualBasicSettings vbs = new VisualBasicSettings(); foreach (var arg in Arguments.Keys) { Type importType = arg.ArgumentType; if (importType == null) { continue; } while (importType != null && importType != typeof(object)) { string ns = importType.Namespace; if (vbs.ImportReferences.Count((i) => i.Import.Equals(ns)) == 0) { VisualBasicImportReference theReference = new VisualBasicImportReference { Assembly = importType.Assembly.FullName, Import = ns }; vbs.ImportReferences.Add(theReference); } importType = importType.BaseType; } foreach (Type interfaceType in arg.ArgumentType.GetInterfaces()) { string interfaceNs = interfaceType.Namespace; if (vbs.ImportReferences.Count((i) => i.Import.Equals(interfaceNs)) == 0) { VisualBasicImportReference interfaceReference = new VisualBasicImportReference { Assembly = interfaceType.Assembly.FullName, Import = interfaceNs }; vbs.ImportReferences.Add(interfaceReference); } } } return(vbs); }
static void CacheLoadedAssembly(Assembly assembly) { // this VBImportReference is only used as an entry to the xmlnsMappings cache // and is never meant to be Xaml serialized. // those VBImportReferences that are to be Xaml serialized are created by Clone() method. XmlnsDefinitionAttribute[] attributes = (XmlnsDefinitionAttribute[])assembly.GetCustomAttributes(typeof(XmlnsDefinitionAttribute), false); string assemblyName = assembly.FullName; XmlnsMapping mapping; // Obtain the lock that is protecting the xmlnsMappings dictionary because we are going // to access it and potentially modify it here. Do the lock here, rather than requiring // the caller to do it because GetCustomAttributes may end up triggering another // assembly load during OnAssemblyLoaded processing, which could cause a deadlock with // a lock used when loading an assembly that is out of our control. One caller - EnsureInitialized - // will get the lock before calling this. But that's okay because locking again on the same thread // works. (DevDiv 251926) // Note that CollectXmlNamespacesAndAssemblies also does this lock because it calls // WrapCachedMapping, which also access and potentially modifieds the xmlnsMapping dictionary. lock (XmlnsMappingsLockObject) { for (int i = 0; i < attributes.Length; ++i) { XNamespace xmlns = XNamespace.Get(attributes[i].XmlNamespace); if (!xmlnsMappings.TryGetValue(xmlns, out mapping)) { mapping.ImportReferences = new HashSet <VisualBasicImportReference>(); xmlnsMappings[xmlns] = mapping; } VisualBasicImportReference newImportReference = new VisualBasicImportReference { Assembly = assemblyName, Import = attributes[i].ClrNamespace, Xmlns = xmlns, }; // early binding the assembly // this leads to the short-cut, skipping the normal assembly resolution routine newImportReference.EarlyBoundAssembly = assembly; mapping.ImportReferences.Add(newImportReference); } } }
private static void CacheLoadedAssembly(Assembly assembly) { XmlnsDefinitionAttribute[] customAttributes = (XmlnsDefinitionAttribute[])assembly.GetCustomAttributes(typeof(XmlnsDefinitionAttribute), false); string fullName = assembly.FullName; for (int i = 0; i < customAttributes.Length; i++) { VisualBasicExpressionConverter.XmlnsMapping mapping; XNamespace key = XNamespace.Get(customAttributes[i].XmlNamespace); if (!xmlnsMappings.TryGetValue(key, out mapping)) { mapping.ImportReferences = new System.Collections.Generic.HashSet <VisualBasicImportReference>(); xmlnsMappings[key] = mapping; } VisualBasicImportReference item = new VisualBasicImportReference { Assembly = fullName, Import = customAttributes[i].ClrNamespace, Xmlns = key }; item.EarlyBoundAssembly = assembly; mapping.ImportReferences.Add(item); } }
private void AddDynamicAssembly(Assembly asm) { AssemblyContextControlItem acci = (AssemblyContextControlItem)(_WorkflowDesigner.Context.Items.GetValue(typeof(AssemblyContextControlItem))); var root = GetRootElement(); var fullname = asm.FullName; if (null == root) { return; } VisualBasicSettings vbs = VisualBasic.GetSettings(root) ?? new VisualBasicSettings(); var namespaces = (from type in asm.GetTypes() select type.Namespace).Distinct(); foreach (var name in namespaces) { var import = new VisualBasicImportReference() { Assembly = fullname, Import = name }; vbs.ImportReferences.Add(import); } VisualBasic.SetSettings(root, vbs); }
public static Activity CreatePrecompiledVisualBasicReference(Type targetType, string expressionText, IEnumerable<string> namespaces, IEnumerable<string> referencedAssemblies, LocationReferenceEnvironment environment, out Type returnType, out SourceExpressionException compileError, out VisualBasicSettings vbSettings) { LambdaExpression lambda = null; HashSet<string> namespacesSet = new HashSet<string>(); HashSet<AssemblyName> assembliesSet = new HashSet<AssemblyName>(); compileError = null; returnType = null; if (namespaces != null) { foreach (string ns in namespaces) { if (ns != null) { namespacesSet.Add(ns); } } } if (referencedAssemblies != null) { foreach (string assm in referencedAssemblies) { if (assm != null) { assembliesSet.Add(new AssemblyName(assm)); } } } VisualBasicHelper vbhelper = new VisualBasicHelper(expressionText, assembliesSet, namespacesSet); if (targetType == null) { try { lambda = vbhelper.CompileNonGeneric(environment); if (lambda != null) { // inspect the expressionTree to see if it is a valid location expression(L-value) string extraErrorMessage; if (!ExpressionUtilities.IsLocation(lambda, targetType, out extraErrorMessage)) { string errorMessage = SR.InvalidLValueExpression; if (extraErrorMessage != null) { errorMessage += ":" + extraErrorMessage; } throw FxTrace.Exception.AsError( new SourceExpressionException(SR.CompilerErrorSpecificExpression(expressionText, errorMessage))); } returnType = lambda.ReturnType; } } catch (SourceExpressionException e) { compileError = e; returnType = typeof(object); } targetType = returnType; } else { MethodInfo genericCompileMethod = typeof(VisualBasicHelper).GetMethod("Compile", new Type[] { typeof(LocationReferenceEnvironment) }); genericCompileMethod = genericCompileMethod.MakeGenericMethod(new Type[] { targetType }); try { lambda = (LambdaExpression)genericCompileMethod.Invoke(vbhelper, new object[] { environment }); // inspect the expressionTree to see if it is a valid location expression(L-value) string extraErrorMessage = null; if (!ExpressionUtilities.IsLocation(lambda, targetType, out extraErrorMessage)) { string errorMessage = SR.InvalidLValueExpression; if (extraErrorMessage != null) { errorMessage += ":" + extraErrorMessage; } throw FxTrace.Exception.AsError( new SourceExpressionException(SR.CompilerErrorSpecificExpression(expressionText, errorMessage))); } returnType = targetType; } catch (SourceExpressionException e) { compileError = e; returnType = typeof(object); } catch (TargetInvocationException e) { SourceExpressionException se = e.InnerException as SourceExpressionException; if (se != null) { compileError = se; returnType = typeof(object); } else { throw FxTrace.Exception.AsError(e.InnerException); } } } vbSettings = new VisualBasicSettings(); if (lambda != null) { HashSet<Type> typeReferences = new HashSet<Type>(); FindTypeReferences(lambda.Body, typeReferences); foreach (Type type in typeReferences) { Assembly tassembly = type.Assembly; if (tassembly.IsDynamic) { continue; } string assemblyName = VisualBasicHelper.GetFastAssemblyName(tassembly).Name; VisualBasicImportReference importReference = new VisualBasicImportReference { Assembly = assemblyName, Import = type.Namespace }; vbSettings.ImportReferences.Add(importReference); } } Type concreteHelperType = VisualBasicExpressionFactoryType.MakeGenericType(targetType); VisualBasicExpressionFactory expressionFactory = (VisualBasicExpressionFactory)Activator.CreateInstance(concreteHelperType); return expressionFactory.CreateVisualBasicReference(expressionText); }
public Activity CreatePrecompiledValue(Type targetType, string expressionText, IEnumerable <string> namespaces, IEnumerable <string> referencedAssemblies, LocationReferenceEnvironment environment, out Type returnType, out SourceExpressionException compileError, out VisualBasicSettings vbSettings) { LambdaExpression lambda = null; HashSet <string> namespacesSet = new HashSet <string>(); HashSet <AssemblyName> assembliesSet = new HashSet <AssemblyName>(); compileError = null; returnType = null; if (namespaces != null) { foreach (string ns in namespaces) { if (ns != null) { namespacesSet.Add(ns); } } } if (referencedAssemblies != null) { foreach (string assm in referencedAssemblies) { if (assm != null) { assembliesSet.Add(new AssemblyName(assm)); } } } var compilerHelper = CreateJitCompilerHelper(expressionText, assembliesSet, namespacesSet); if (targetType == null) { try { lambda = compilerHelper.CompileNonGeneric(environment); if (lambda != null) { returnType = lambda.ReturnType; } } catch (SourceExpressionException e) { compileError = e; returnType = typeof(object); } targetType = returnType; } else { MethodInfo genericCompileMethod = compilerHelper.GetType().GetMethod("Compile", new Type[] { typeof(LocationReferenceEnvironment) }); genericCompileMethod = genericCompileMethod.MakeGenericMethod(new Type[] { targetType }); try { lambda = (LambdaExpression)genericCompileMethod.Invoke(compilerHelper, new object[] { environment }); returnType = targetType; } catch (TargetInvocationException e) { SourceExpressionException se = e.InnerException as SourceExpressionException; if (se != null) { compileError = se; returnType = typeof(object); } else { throw FxTrace.Exception.AsError(e.InnerException); } } } vbSettings = new VisualBasicSettings(); if (lambda != null) { HashSet <Type> typeReferences = new HashSet <Type>(); FindTypeReferences(lambda.Body, typeReferences); foreach (Type type in typeReferences) { Assembly tassembly = type.Assembly; if (tassembly.IsDynamic) { continue; } string assemblyName = AssemblyReference.GetFastAssemblyName(tassembly).Name; VisualBasicImportReference importReference = new VisualBasicImportReference { Assembly = assemblyName, Import = type.Namespace }; vbSettings.ImportReferences.Add(importReference); } } Type concreteHelperType = ExpressionFactoryType.MakeGenericType(targetType); ExpressionFactory expressionFactory = (ExpressionFactory)Activator.CreateInstance(concreteHelperType); return(expressionFactory.CreateValue(expressionText)); }
internal ReadOnlyVisualBasicImportReference(VisualBasicImportReference referenceToWrap) { this.wrappedReference = referenceToWrap; }
public static VisualBasicSettings CollectXmlNamespacesAndAssemblies(ITypeDescriptorContext context) { // access XamlSchemaContext.ReferenceAssemblies // for the Compiled Xaml scenario IList <Assembly> xsCtxReferenceAssemblies = null; IXamlSchemaContextProvider xamlSchemaContextProvider = context.GetService(typeof(IXamlSchemaContextProvider)) as IXamlSchemaContextProvider; if (xamlSchemaContextProvider != null && xamlSchemaContextProvider.SchemaContext != null) { xsCtxReferenceAssemblies = xamlSchemaContextProvider.SchemaContext.ReferenceAssemblies; if (xsCtxReferenceAssemblies != null && xsCtxReferenceAssemblies.Count == 0) { xsCtxReferenceAssemblies = null; } } VisualBasicSettings settings = null; IXamlNamespaceResolver namespaceResolver = (IXamlNamespaceResolver)context.GetService(typeof(IXamlNamespaceResolver)); if (namespaceResolver == null) { return(null); } lock (AssemblyCache.XmlnsMappingsLockObject) { // Fetch xmlnsMappings for the prefixes returned by the namespaceResolver service foreach (NamespaceDeclaration prefix in namespaceResolver.GetNamespacePrefixes()) { ReadOnlyXmlnsMapping mapping; WrapCachedMapping(prefix, out mapping); if (!mapping.IsEmpty) { if (settings == null) { settings = new VisualBasicSettings(); } if (!mapping.IsEmpty) { foreach (ReadOnlyVisualBasicImportReference importReference in mapping.ImportReferences) { if (xsCtxReferenceAssemblies != null) { // this is "compiled Xaml" VisualBasicImportReference newImportReference; if (importReference.EarlyBoundAssembly != null) { if (xsCtxReferenceAssemblies.Contains(importReference.EarlyBoundAssembly)) { newImportReference = importReference.Clone(); newImportReference.EarlyBoundAssembly = importReference.EarlyBoundAssembly; settings.ImportReferences.Add(newImportReference); } continue; } for (int i = 0; i < xsCtxReferenceAssemblies.Count; i++) { AssemblyName xsCtxAssemblyName = VisualBasicHelper.GetFastAssemblyName(xsCtxReferenceAssemblies[i]); if (importReference.AssemblySatisfiesReference(xsCtxAssemblyName)) { // bind this assembly early to the importReference // so later AssemblyName resolution can be skipped newImportReference = importReference.Clone(); newImportReference.EarlyBoundAssembly = xsCtxReferenceAssemblies[i]; settings.ImportReferences.Add(newImportReference); break; } } } else { // this is "loose Xaml" VisualBasicImportReference newImportReference = importReference.Clone(); if (importReference.EarlyBoundAssembly != null) { // VBImportReference.Clone() method deliberately doesn't copy // its EarlyBoundAssembly to the cloned instance. // we need to explicitly copy the original's EarlyBoundAssembly newImportReference.EarlyBoundAssembly = importReference.EarlyBoundAssembly; } settings.ImportReferences.Add(newImportReference); } } } } } } return(settings); }
public static VisualBasicSettings CollectXmlNamespacesAndAssemblies(ITypeDescriptorContext context) { IList <Assembly> referenceAssemblies = null; IXamlSchemaContextProvider service = context.GetService(typeof(IXamlSchemaContextProvider)) as IXamlSchemaContextProvider; if ((service != null) && (service.SchemaContext != null)) { referenceAssemblies = service.SchemaContext.ReferenceAssemblies; if ((referenceAssemblies != null) && (referenceAssemblies.Count == 0)) { referenceAssemblies = null; } } VisualBasicSettings settings = null; IXamlNamespaceResolver resolver = (IXamlNamespaceResolver)context.GetService(typeof(IXamlNamespaceResolver)); if (resolver == null) { return(null); } lock (AssemblyCache.XmlnsMappings) { foreach (System.Xaml.NamespaceDeclaration declaration in resolver.GetNamespacePrefixes()) { XmlnsMapping mapping; XNamespace key = XNamespace.Get(declaration.Namespace); if (!AssemblyCache.XmlnsMappings.TryGetValue(key, out mapping)) { Match match = assemblyQualifiedNamespaceRegex.Match(declaration.Namespace); if (match.Success) { mapping.ImportReferences = new System.Collections.Generic.HashSet <VisualBasicImportReference>(); VisualBasicImportReference item = new VisualBasicImportReference { Assembly = match.Groups["assembly"].Value, Import = match.Groups["namespace"].Value, Xmlns = key }; mapping.ImportReferences.Add(item); } else { mapping.ImportReferences = new System.Collections.Generic.HashSet <VisualBasicImportReference>(); } AssemblyCache.XmlnsMappings[key] = mapping; } if (!mapping.IsEmpty) { if (settings == null) { settings = new VisualBasicSettings(); } foreach (VisualBasicImportReference reference2 in mapping.ImportReferences) { if (referenceAssemblies != null) { VisualBasicImportReference reference3; AssemblyName assemblyName = reference2.AssemblyName; if (reference2.EarlyBoundAssembly != null) { if (referenceAssemblies.Contains(reference2.EarlyBoundAssembly)) { reference3 = reference2.Clone(); reference3.EarlyBoundAssembly = reference2.EarlyBoundAssembly; settings.ImportReferences.Add(reference3); } } else { for (int i = 0; i < referenceAssemblies.Count; i++) { if (AssemblySatisfiesReference(VisualBasicHelper.GetFastAssemblyName(referenceAssemblies[i]), assemblyName)) { reference3 = reference2.Clone(); reference3.EarlyBoundAssembly = referenceAssemblies[i]; settings.ImportReferences.Add(reference3); break; } } } } else { VisualBasicImportReference reference4 = reference2.Clone(); if (reference2.EarlyBoundAssembly != null) { reference4.EarlyBoundAssembly = reference2.EarlyBoundAssembly; } settings.ImportReferences.Add(reference4); } } } } } return(settings); }
public static Activity CreatePrecompiledVisualBasicReference(Type targetType, string expressionText, IEnumerable<string> namespaces, IEnumerable<string> referencedAssemblies, LocationReferenceEnvironment environment, out Type returnType, out SourceExpressionException compileError, out VisualBasicSettings vbSettings) { LambdaExpression expression = null; HashSet<string> namespaceImportsNames = new HashSet<string>(); HashSet<AssemblyName> refAssemNames = new HashSet<AssemblyName>(); compileError = null; returnType = null; if (namespaces != null) { foreach (string str in namespaces) { if (str != null) { namespaceImportsNames.Add(str); } } } if (referencedAssemblies != null) { foreach (string str2 in referencedAssemblies) { if (str2 != null) { refAssemNames.Add(new AssemblyName(str2)); } } } VisualBasicHelper helper = new VisualBasicHelper(expressionText, refAssemNames, namespaceImportsNames); if (targetType == null) { try { expression = helper.CompileNonGeneric(environment); if (expression != null) { string str3; if (!ExpressionUtilities.IsLocation(expression, targetType, out str3)) { string invalidLValueExpression = System.Activities.SR.InvalidLValueExpression; if (str3 != null) { invalidLValueExpression = invalidLValueExpression + ":" + str3; } throw FxTrace.Exception.AsError(new SourceExpressionException(System.Activities.SR.CompilerErrorSpecificExpression(expressionText, invalidLValueExpression))); } returnType = expression.ReturnType; } } catch (SourceExpressionException exception) { compileError = exception; returnType = typeof(object); } targetType = returnType; } else { MethodInfo info = typeof(VisualBasicHelper).GetMethod("Compile", new Type[] { typeof(LocationReferenceEnvironment) }).MakeGenericMethod(new Type[] { targetType }); try { expression = (LambdaExpression) info.Invoke(helper, new object[] { environment }); string extraErrorMessage = null; if (!ExpressionUtilities.IsLocation(expression, targetType, out extraErrorMessage)) { string str6 = System.Activities.SR.InvalidLValueExpression; if (extraErrorMessage != null) { str6 = str6 + ":" + extraErrorMessage; } throw FxTrace.Exception.AsError(new SourceExpressionException(System.Activities.SR.CompilerErrorSpecificExpression(expressionText, str6))); } returnType = targetType; } catch (SourceExpressionException exception2) { compileError = exception2; returnType = typeof(object); } catch (TargetInvocationException exception3) { SourceExpressionException innerException = exception3.InnerException as SourceExpressionException; if (innerException == null) { throw FxTrace.Exception.AsError(exception3.InnerException); } compileError = innerException; returnType = typeof(object); } } vbSettings = new VisualBasicSettings(); if (expression != null) { HashSet<Type> typeReferences = new HashSet<Type>(); FindTypeReferences(expression.Body, typeReferences); foreach (Type type in typeReferences) { Assembly assembly = type.Assembly; if (!assembly.IsDynamic) { string name = VisualBasicHelper.GetFastAssemblyName(assembly).Name; VisualBasicImportReference item = new VisualBasicImportReference { Assembly = name, Import = type.Namespace }; vbSettings.ImportReferences.Add(item); } } } VisualBasicExpressionFactory factory = (VisualBasicExpressionFactory) Activator.CreateInstance(VisualBasicExpressionFactoryType.MakeGenericType(new Type[] { targetType })); return factory.CreateVisualBasicReference(expressionText); }