//Runs when item is clicked. //We should be able to avoid loading actual ToolboxItem instances and their //associated assemblies until they are activated. public override void Activate(object host) { IDesignerHost desHost = host as IDesignerHost; if (desHost == null) { throw new Exception("This ToolboxItem should not have been shown for this host. System.Drawing.Design.ToolboxItem requires a host of type System.ComponentModel.Design.IDesignerHost"); } //web controls have sample HTML that need to be deserialised //TODO: Fix WebControlToolboxItem so we don't have to mess around with type lookups and attributes here if ((item is System.Web.UI.Design.WebControlToolboxItem) && host is AspNetEdit.Editor.ComponentModel.DesignerHost) { AspNetEdit.Editor.ComponentModel.DesignerHost aspDesHost = (AspNetEdit.Editor.ComponentModel.DesignerHost)desHost; if (item.AssemblyName != null && item.TypeName != null) { //look up and register the type ITypeResolutionService typeRes = (ITypeResolutionService)aspDesHost.GetService(typeof(ITypeResolutionService)); typeRes.ReferenceAssembly(item.AssemblyName); Type controlType = typeRes.GetType(item.TypeName, true); //read the WebControlToolboxItem data from the attribute AttributeCollection atts = TypeDescriptor.GetAttributes(controlType); System.Web.UI.ToolboxDataAttribute tda = (System.Web.UI.ToolboxDataAttribute)atts[typeof(System.Web.UI.ToolboxDataAttribute)]; //if it's present if (tda != null && tda.Data.Length > 0) { //look up the tag's prefix and insert it into the data System.Web.UI.Design.IWebFormReferenceManager webRef = aspDesHost.GetService(typeof(System.Web.UI.Design.IWebFormReferenceManager)) as System.Web.UI.Design.IWebFormReferenceManager; if (webRef == null) { throw new Exception("Host does not provide an IWebFormReferenceManager"); } string aspText = String.Format(tda.Data, webRef.GetTagPrefix(controlType)); System.Diagnostics.Trace.WriteLine("Toolbox processing ASP.NET item data: " + aspText); //and add it to the document aspDesHost.RootDocument.DeserializeAndAdd(aspText); return; } } } //No ToolboxDataAttribute? Get the ToolboxItem to create the components itself item.CreateComponents(desHost); }
private void handleToolboxNode(ItemToolboxNode node) { ToolboxItemToolboxNode tiNode = node as ToolboxItemToolboxNode; if (tiNode != null) { //load the type into this process and get the ToolboxItem tiNode.Type.Load(); System.Drawing.Design.ToolboxItem ti = tiNode.GetToolboxItem(); //web controls have sample HTML that need to be deserialised, in a ToolboxDataAttribute //TODO: Fix WebControlToolboxItem and (mono classlib's use of it) so we don't have to mess around with type lookups and attributes here if (ti.AssemblyName != null && ti.TypeName != null) { //look up and register the type ITypeResolutionService typeRes = (ITypeResolutionService)designerHost.GetService(typeof(ITypeResolutionService)); typeRes.ReferenceAssembly(ti.AssemblyName); Type controlType = typeRes.GetType(ti.TypeName, true); //read the WebControlToolboxItem data from the attribute AttributeCollection atts = TypeDescriptor.GetAttributes(controlType); System.Web.UI.ToolboxDataAttribute tda = (System.Web.UI.ToolboxDataAttribute)atts [typeof(System.Web.UI.ToolboxDataAttribute)]; //if it's present if (tda != null && tda.Data.Length > 0) { //look up the tag's prefix and insert it into the data WebFormReferenceManager webRef = designerHost.GetService(typeof(WebFormReferenceManager)) as WebFormReferenceManager; if (webRef == null) { throw new Exception("Host does not provide an IWebFormReferenceManager"); } string aspText = String.Format(tda.Data, webRef.GetTagPrefix(controlType)); System.Diagnostics.Trace.WriteLine("Toolbox processing ASP.NET item data: " + aspText); //and add it to the document designerHost.RootDocument.InsertFragment(aspText); return; } } //No ToolboxDataAttribute? Get the ToolboxItem to create the components itself ti.CreateComponents(designerHost); } }
protected virtual Type GetType(IDesignerHost host, AssemblyName assemblyName, string typeName, bool reference) { if (typeName == null) { throw new ArgumentNullException("typeName"); } if (host == null) { return(null); } //get ITypeResolutionService from host, as we have no other IServiceProvider here ITypeResolutionService typeRes = host.GetService(typeof(ITypeResolutionService)) as ITypeResolutionService; Type type = null; if (typeRes != null) { //TODO: Using Assembly loader to throw errors. Silent fail and return null? typeRes.GetAssembly(assemblyName, true); if (reference) { typeRes.ReferenceAssembly(assemblyName); } type = typeRes.GetType(typeName, true); } else { Assembly assembly = Assembly.Load(assemblyName); if (assembly != null) { type = assembly.GetType(typeName); } } return(type); }
/// <summary> /// Gets a type space object from a JSON representation. /// </summary> /// <param name="expression">JSON representation of the type space.</param> /// <param name="typeResolutionService">Type resolution service. Can be null.</param> /// <returns>Type space for the given JSON representation.</returns> public static TypeSpace FromJson(Json.Expression expression, ITypeResolutionService typeResolutionService) { // // Referencing mscorlib for default lookups. See SimpleTypeDef.FromJson for the // special treatment of this assembly in order to shorten names in serialization // payload. // if (typeResolutionService != null) { typeResolutionService.ReferenceAssembly(typeof(int).Assembly.GetName() /* mscorlib */); } // // Inverse of ToJson. // var defs = ((Json.ArrayExpression)expression).Elements; // // First, get enough type reference objects to support the full range of ordinals. // var typeRefs = Enumerable.Range(0, defs.Count).Select(i => new TypeRef(i)).ToArray(); // // Using a findRef delegate, we can now import the type definitions. Notice this // assumes that type references are in a fixed zero-based range. // var findRef = new Func <Json.Expression, TypeRef>(r => typeRefs[TypeRef.FromJson(r).Ordinal]); var typeDefs = defs.Select(def => TypeDef.FromJson(def, findRef, typeResolutionService)).ToArray(); // // Next, we associate all of the type references with their corresponding definition, // again based on ordinal number indexes. // var j = 0; foreach (var typeDef in typeDefs) { var typeRef = typeRefs[j++]; typeRef.Definition = typeDef; } // // The first phase of defining the type space consists of compilation of the types, // resulting in recreation of structural types. Entries are added to the type def // table to allow for Lookup calls during deserialization of the expression tree. // var res = new TypeSpace(); var structuralTypes = new List <StructuralTypeDef>(); foreach (var typeRef in typeRefs) { var def = typeRef.Definition; res._typeDefs[typeRef] = def; def.Compile(res._compiler, typeResolutionService); // // Structural types need a separate compilation stage in order to "freeze" the // reconstructed types. See comment below for more information. // if (def is StructuralTypeDef std) { structuralTypes.Add(std); } } // // Second stage of compilation, triggering the CreateType on TypeBuilder to cause // final construction of the types. // foreach (var structuralType in structuralTypes) { structuralType.ToType(); // side-effect described above } // // The following piece of code can be re-enabled for debugging purposes in order to // see a Type-based index for type references. This is only needed for serialization, // so we eliminate this step for deserialization. // /* * foreach (var typeRef in typeRefs) * { * var type = typeRef.Definition.ToType(); * res._typeRefs[type] = typeRef; * } */ return(res); }
public static Activity[] DeserializeActivitiesFromDataObject(IServiceProvider serviceProvider, IDataObject dataObj, bool addReference) { IDesignerHost designerHost = (IDesignerHost)serviceProvider.GetService(typeof(IDesignerHost)); if (designerHost == null) { throw new InvalidOperationException("IDesignerHost is missing."); } if (dataObj == null) { return new Activity[] { } } ; object data = dataObj.GetData(CF_DESIGNER); ICollection activities = null; if (data is Stream) { BinaryFormatter formatter = new BinaryFormatter(); ((Stream)data).Seek(0, SeekOrigin.Begin); object serializationData = formatter.Deserialize((Stream)data); if (serializationData is SerializationStore) { // get component serialization service ComponentSerializationService css = serviceProvider.GetService(typeof(ComponentSerializationService)) as ComponentSerializationService; if (css == null) { throw new Exception("ComponentSerializationService is missing."); } // deserialize data activities = css.Deserialize((SerializationStore)serializationData); } } else { // Now check for a toolbox item. IToolboxService ts = (IToolboxService)serviceProvider.GetService(typeof(IToolboxService)); if (ts != null && ts.IsSupported(dataObj, designerHost)) { ToolboxItem toolBoxItem = ts.DeserializeToolboxItem(dataObj, designerHost); if (toolBoxItem != null) { // this will make sure that we add the assembly reference to project if (addReference && toolBoxItem.AssemblyName != null) { ITypeResolutionService trs = serviceProvider.GetService(typeof(ITypeResolutionService)) as ITypeResolutionService; if (trs != null) { trs.ReferenceAssembly(toolBoxItem.AssemblyName); } } ActivityToolboxItem ActivityToolboxItem = toolBoxItem as ActivityToolboxItem; if (addReference && ActivityToolboxItem != null) { activities = ActivityToolboxItem.CreateComponentsWithUI(designerHost); } else { activities = toolBoxItem.CreateComponents(designerHost); } } } } return((activities != null) ? (Activity[])(new ArrayList(activities).ToArray(typeof(Activity))) : new Activity[] { }); }
protected virtual Type GetType(IDesignerHost host, AssemblyName assemblyName, string typeName, bool reference) { ITypeResolutionService ts = null; Type type = null; if (typeName == null) { throw new ArgumentNullException("typeName"); } if (host != null) { ts = (ITypeResolutionService)host.GetService(typeof(ITypeResolutionService)); } if (ts != null) { if (reference) { if (assemblyName != null) { ts.ReferenceAssembly(assemblyName); type = ts.GetType(typeName); } else { // Just try loading the type. If we succeed, then use this as the // reference. type = ts.GetType(typeName); if (type == null) { type = Type.GetType(typeName); } if (type != null) { ts.ReferenceAssembly(type.Assembly.GetName()); } } } else { if (assemblyName != null) { Assembly a = ts.GetAssembly(assemblyName); if (a != null) { type = a.GetType(typeName); } } if (type == null) { type = ts.GetType(typeName); } } } else { if (!String.IsNullOrEmpty(typeName)) { if (assemblyName != null) { Assembly a = null; try { a = Assembly.Load(assemblyName); } catch (FileNotFoundException) { } catch (BadImageFormatException) { } catch (IOException) { } if (a == null && assemblyName.CodeBase != null && assemblyName.CodeBase.Length > 0) { try { a = Assembly.LoadFrom(assemblyName.CodeBase); } catch (FileNotFoundException) { } catch (BadImageFormatException) { } catch (IOException) { } } if (a != null) { type = a.GetType(typeName); } } if (type == null) { type = Type.GetType(typeName, false); } } } return(type); }
/// <summary> /// Creates the necessary components associated with this data adapter instance /// </summary> /// <param name="host">The designer host</param> /// <returns>The components created by this toolbox item</returns> protected override IComponent[] CreateComponentsCore(IDesignerHost host) { DbProviderFactory fact = DbProviderFactories.GetFactory("Npgsql"); DbDataAdapter dataAdapter = fact.CreateDataAdapter(); IContainer container = host.Container; using (DbCommand adapterCommand = fact.CreateCommand()) { adapterCommand.DesignTimeVisible = false; dataAdapter.SelectCommand = (DbCommand)((ICloneable)adapterCommand).Clone(); container.Add(dataAdapter.SelectCommand, GenerateName(container, "SelectCommand")); dataAdapter.InsertCommand = (DbCommand)((ICloneable)adapterCommand).Clone(); container.Add(dataAdapter.InsertCommand, GenerateName(container, "InsertCommand")); dataAdapter.UpdateCommand = (DbCommand)((ICloneable)adapterCommand).Clone(); container.Add(dataAdapter.UpdateCommand, GenerateName(container, "UpdateCommand")); dataAdapter.DeleteCommand = (DbCommand)((ICloneable)adapterCommand).Clone(); container.Add(dataAdapter.DeleteCommand, GenerateName(container, "DeleteCommand")); } ITypeResolutionService typeResService = (ITypeResolutionService)host.GetService(typeof(ITypeResolutionService)); if (typeResService != null) { typeResService.ReferenceAssembly(dataAdapter.GetType().Assembly.GetName()); } container.Add(dataAdapter); List <IComponent> list = new List <IComponent>(); list.Add(dataAdapter); // Show the connection wizard if we have a type for it if (_wizard != null) { using (Form wizard = (Form)Activator.CreateInstance(_wizard, new object[] { host, dataAdapter })) { wizard.ShowDialog(); } } if (dataAdapter.SelectCommand != null) { list.Add(dataAdapter.SelectCommand); } if (dataAdapter.InsertCommand != null) { list.Add(dataAdapter.InsertCommand); } if (dataAdapter.DeleteCommand != null) { list.Add(dataAdapter.DeleteCommand); } if (dataAdapter.UpdateCommand != null) { list.Add(dataAdapter.UpdateCommand); } return(list.ToArray()); }
protected virtual Type GetType(IDesignerHost host, System.Reflection.AssemblyName assemblyName, string typeName, bool reference) { ITypeResolutionService service = null; Type type = null; if (typeName == null) { throw new ArgumentNullException("typeName"); } if (host != null) { service = (ITypeResolutionService)host.GetService(typeof(ITypeResolutionService)); } if (service != null) { if (reference) { if (assemblyName != null) { service.ReferenceAssembly(assemblyName); return(service.GetType(typeName)); } type = service.GetType(typeName); if (type == null) { type = Type.GetType(typeName); } if (type != null) { service.ReferenceAssembly(type.Assembly.GetName()); } return(type); } if (assemblyName != null) { Assembly assembly = service.GetAssembly(assemblyName); if (assembly != null) { type = assembly.GetType(typeName); } } if (type == null) { type = service.GetType(typeName); } return(type); } if (!string.IsNullOrEmpty(typeName)) { if (assemblyName != null) { Assembly assembly2 = null; try { assembly2 = Assembly.Load(assemblyName); } catch (FileNotFoundException) { } catch (BadImageFormatException) { } catch (IOException) { } if (((assembly2 == null) && (assemblyName.CodeBase != null)) && (assemblyName.CodeBase.Length > 0)) { try { assembly2 = Assembly.LoadFrom(assemblyName.CodeBase); } catch (FileNotFoundException) { } catch (BadImageFormatException) { } catch (IOException) { } } if (assembly2 != null) { type = assembly2.GetType(typeName); } } if (type == null) { type = Type.GetType(typeName, false); } } return(type); }
CreateComponentsCore(IDesignerHost host) { const string prefix = VSNETConst.shortTitle; // "Ingres" IContainer designerHostContainer = host.Container; ArrayList newComponents = new ArrayList(); ArrayList newCommandComponents = new ArrayList(); string name; IngresConnection connection; IngresCommand providerCommand; if (designerHostContainer == null) // safety check { return(null); } IngresDataAdapter dataAdapter = new IngresDataAdapter(); // Once a reference to an assembly has been added to this service, // this service can load types from names that // do not specify an assembly. ITypeResolutionService resService = (ITypeResolutionService)host.GetService(typeof(ITypeResolutionService)); System.Reflection.Assembly assembly = dataAdapter.GetType().Module.Assembly; if (resService != null) { resService.ReferenceAssembly(assembly.GetName()); // set the assembly name to load types from } name = DesignerNameManager.CreateUniqueName( designerHostContainer, prefix, "DataAdapter"); try { designerHostContainer.Add(dataAdapter, name); newComponents.Add(dataAdapter); } catch (ArgumentException ex) { string exMsg = ex.ToString() + "\nRemove IngresDataAdapter component '" + name + "'" + " that is defined outside of the designer. "; MessageBox.Show(exMsg, "Add " + name + " failed"); return(null); } string [] commandName = new String [] { "SelectCommand", "InsertCommand", "UpdateCommand", "DeleteCommand", }; providerCommand = new IngresCommand(); // SELECT newCommandComponents.Add(providerCommand); providerCommand.DesignTimeVisible = false; dataAdapter.SelectCommand = providerCommand; providerCommand = new IngresCommand(); // INSERT newCommandComponents.Add(providerCommand); providerCommand.DesignTimeVisible = false; dataAdapter.InsertCommand = providerCommand; providerCommand = new IngresCommand(); // UPDATE newCommandComponents.Add(providerCommand); providerCommand.DesignTimeVisible = false; dataAdapter.UpdateCommand = providerCommand; providerCommand = new IngresCommand(); // DELETE newCommandComponents.Add(providerCommand); providerCommand.DesignTimeVisible = false; dataAdapter.DeleteCommand = providerCommand; // invoke the wizard to create the connection and query // pass VS.NET IDesignerHost host so we can add Connection DataAdapterWizard wizard = new DataAdapterWizard(dataAdapter, host); // The DataAdapterWizard called the ConnectionEditor and // gave the Wizard and Editor a chance to create the Connection // object and SELECT statement, or to cancel out. // If not cancelled, add the Connection object to // the VS.NET component tray if not already done. if (dataAdapter.SelectCommand != null && dataAdapter.SelectCommand.Connection != null) { connection = dataAdapter.SelectCommand.Connection; name = DesignerNameManager.CreateUniqueName( designerHostContainer, prefix, "Connection"); //add the connection to the VS.NET component tray designerHostContainer.Add(connection, name); } // add the four Command objects (each with a unique name) // to the designer's components tray for (int i = 0; i < 4; i++) { name = DesignerNameManager.CreateUniqueName( designerHostContainer, prefix, commandName[i]); designerHostContainer.Add( (IComponent)newCommandComponents[i], name); } // end for loop thru command components string s = "Creating component list for " + this.GetType() + "?\n"; return((IComponent[])(newComponents.ToArray(typeof(IComponent)))); //return base.CreateComponentsCore(host); } // CreateComponentsCore
// InvokeMethods the currently selected method, if any, when // the InvokeMethod button is pressed. private void InvokeMethod(object sender, EventArgs e) { // If the GetAssembly or GetPathofAssembly radio button // is selected. if (this.radioButton1.Checked || this.radioButton2.Checked || this.radioButton4.Checked) { if (this.entryBox.Text.Length == 0) { // If there is no assembly name specified, display status message. this.infoBox.Text = "You must enter the name of the assembly to retrieve."; } else if (rs != null) { // Create a System.Reflection.AssemblyName // for the entered text. AssemblyName name = new AssemblyName(); name.Name = this.entryBox.Text.Trim(); // If the GetAssembly radio button is checked... if (this.radioButton1.Checked) { // Use the ITypeResolutionService to attempt to // resolve an assembly reference. Assembly a = rs.GetAssembly(name, false); // If an assembly matching the specified name was not // located in the GAC or local project references, // display a message. if (a == null) { this.infoBox.Text = "The " + this.entryBox.Text + " assembly could not be located."; } else { // An assembly matching the specified name was located. // Builds a list of types. Type[] types = a.GetTypes(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < types.Length; i++) { sb.Append(types[i].FullName + "\r\n"); } string path = rs.GetPathOfAssembly(name); // Displays assembly information and a list of types contained in the assembly. this.infoBox.Text = "Assembly located:\r\n\r\n" + a.FullName + "\r\n at: " + path + "\r\n\r\nAssembly types:\r\n\r\n" + sb.ToString(); } } else if (this.radioButton2.Checked) { string path = rs.GetPathOfAssembly(name); if (path != null) { this.infoBox.Text = "Assembly located at:\r\n" + path; } else { this.infoBox.Text = "Assembly was not located."; } } else if (this.radioButton4.Checked) { Assembly a = null; try { // Add a reference to the assembly to the // current project. rs.ReferenceAssembly(name); // Use the ITypeResolutionService to attempt // to resolve an assembly reference. a = rs.GetAssembly(name, false); } catch { // Catch this exception so that the exception // does not interrupt control behavior. } // If an assembly matching the specified name was not // located in the GAC or local project references, // display a message. if (a == null) { this.infoBox.Text = "The " + this.entryBox.Text + " assembly could not be located."; } else { this.infoBox.Text = "A reference to the " + a.FullName + " assembly has been added to the project's referenced assemblies."; } } } } else if (this.radioButton3.Checked) { if (this.entryBox.Text.Length == 0) { // If there is no type name specified, display a // status message. this.infoBox.Text = "You must enter the name of the type to retrieve."; } else if (rs != null) { Type type = null; try { type = rs.GetType(this.entryBox.Text, false, true); } catch { // Consume exceptions raised during GetType call } if (type != null) { // Display type information. this.infoBox.Text = "Type: " + type.FullName + " located.\r\n Namespace: " + type.Namespace + "\r\n" + type.AssemblyQualifiedName; } else { this.infoBox.Text = "Type not located."; } } } }
/// <summary> /// See <see cref="ITypeResolutionService.ReferenceAssembly"/>. /// </summary> public override void ReferenceAssembly(AssemblyName name) { parentLoader.ReferenceAssembly(name); }