SolutionFramework /SolutionFramework/ControlProviders/ToolboxInstaller/Microsoft/VisualStudio/IDE/ToolboxControlsInstaller/XamlAssembly.cs

Language C# Lines 347
MD5 Hash 272816eed477ab579e1d5c8bdf272a4f Estimated Cost $7,045 (why?)
Repository https://SolutionFramework.svn.codeplex.com/svn View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
namespace Microsoft.VisualStudio.IDE.ToolboxControlsInstaller
{
    using Microsoft.VisualStudio;
    using Microsoft.VisualStudio.OLE.Interop;
    using Microsoft.VisualStudio.Shell;
    using Microsoft.VisualStudio.Shell.Interop;
    using Microsoft.Win32;
    using Microsoft.Windows.Design;
    using Microsoft.Windows.Design.Host;
    using Microsoft.Windows.Design.Interaction;
    using Microsoft.Windows.Design.Metadata;
    //using Microsoft.Windows.Design.Metadata.Reflection;
    using MS.Internal.Package;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Globalization;
    using System.IO;
    using System.Reflection;
    using System.Runtime.InteropServices;
    using System.Runtime.Versioning;
    using System.Windows;
    using System.Windows.Forms;

    internal abstract class XamlAssembly : RegisteredAssembly
    {
        private static IsolationProvider _lastIsolationProvider;
        private static FrameworkName _lastTargetFrameworkName;
        private const string TabProperty = "VSTOOLBOXTAB";

        public XamlAssembly(AssemblyName assemblyName) : base(assemblyName)
        {
        }

        public override bool AddItemsToToolbox(System.IServiceProvider provider, RegistryKey applicationRegistryRoot)
        {
            bool flag = false;
            IVsActivityLog service = provider.GetService(typeof(SVsActivityLog)) as IVsActivityLog;
            service.LogEntry(3, this.ToString(), "Toolbox Controls Installer Package attempting to add Toolbox items");
            service.LogEntry(3, this.ToString(), string.Format(CultureInfo.CurrentCulture, "Attempting to get Toolbox items from Assembly {0}", new object[] { base.AssemblyName.FullName }));
            provider.GetService(typeof(Microsoft.VisualStudio.Shell.Interop.SVsFrameworkMultiTargeting));
            IQueryToolboxItems items = (IQueryToolboxItems) this.GetIsolationProvider(provider).CreateRemoteInstance(typeof(IsolationProxy));
            ICollection is2 = null;
            try
            {
                is2 = items.GetToolboxItems(base.AssemblyName, this.FrameworkElementAssemblySpec, new ArrayList(this.ExclusionList.ToArray()));
            }
            catch (Exception exception)
            {
                service.LogEntry(1, this.ToString(), string.Format(CultureInfo.CurrentCulture, "Assembly {0} threw exception {1} when attempting to enumerate Types.", new object[] { base.AssemblyName.FullName, exception.ToString() }));
            }
            if ((is2 == null) || (is2.Count == 0))
            {
                service.LogEntry(2, this.ToString(), string.Format(CultureInfo.CurrentCulture, "Assembly {0} did not contain any WPF/SL toolbox items.", new object[] { base.AssemblyName.FullName }));
                return flag;
            }
            foreach (IDictionary dictionary in is2)
            {
                string typeName = dictionary["CreationTypeName"].ToString();
                string fullTypeName = base.ExtractFullTypeName(typeName);
                string str3 = base.ExtractShortTypeName(typeName);
                service.LogEntry(3, this.ToString(), string.Format(CultureInfo.CurrentCulture, "Attempting to add Toolbox item {0}", new object[] { fullTypeName }));
                string tabName = null;
                if (dictionary.Contains("VSTOOLBOXTAB"))
                {
                    tabName = (string) dictionary["VSTOOLBOXTAB"];
                    dictionary.Remove("VSTOOLBOXTAB");
                }
                byte[] buffer = ToolEncoder.Encode(dictionary);
                OleDataObject dataObj = new OleDataObject();
                dataObj.SetData(System.Windows.DataFormats.GetDataFormat(this.ClipFormat).Name, false, new MemoryStream(buffer));
                TBXITEMINFO itemInfo = new TBXITEMINFO();
                TBXITEMINFO itemInfoAll = new TBXITEMINFO();
                itemInfo.bstrText = str3;
                byte[] buffer2 = (byte[]) dictionary["Bitmap"];
                Bitmap bitmap = null;
                System.Drawing.Color black = System.Drawing.Color.Black;
                if (buffer2 != null)
                {
                    bitmap = new Bitmap(new MemoryStream(buffer2));
                }
                else
                {
                    bitmap = new Bitmap(typeof(XamlAssembly).Assembly.GetManifestResourceStream(typeof(XamlAssembly), "DefaultBitmap.bmp"));
                }
                itemInfoAll = itemInfo;
                if (bitmap != null)
                {
                    if ((bitmap.Height > 0) && (bitmap.Width > 0))
                    {
                        black = bitmap.GetPixel(0, bitmap.Size.Height - 1);
                    }
                    if (black.A < 0xff)
                    {
                        black = System.Drawing.Color.FromArgb(0xff, black);
                    }
                    itemInfo.hBmp = ControlPaint.CreateHBitmap16Bit(bitmap, black);
                    itemInfo.clrTransparent = (uint) ColorTranslator.ToWin32(black);
                    itemInfo.dwFlags = 1;
                    itemInfoAll.hBmp = ControlPaint.CreateHBitmap16Bit(bitmap, black);
                    itemInfoAll.clrTransparent = (uint) ColorTranslator.ToWin32(black);
                    itemInfoAll.dwFlags = 1;
                }
                if (this.AddToToolbox(provider, tabName, this.CreateControl(fullTypeName, dictionary), dataObj, itemInfo, itemInfoAll))
                {
                    flag = true;
                }
            }
            return flag;
        }

        private bool AddToToolbox(System.IServiceProvider provider, string tabName, DictionaryControl control, Microsoft.VisualStudio.OLE.Interop.IDataObject dataObj, TBXITEMINFO itemInfo, TBXITEMINFO itemInfoAll)
        {
            if (dataObj is System.Windows.Forms.IDataObject)
            {
                dataObj = new SettableOleDataObject(dataObj as System.Windows.Forms.IDataObject);
            }
            provider.GetService(typeof(SVsActivityLog));
            string typeName = control.Properties["CreationTypeName"].ToString();
            string szValue = base.ExtractFullTypeName(typeName);
            base.ExtractShortTypeName(typeName);
            if (!string.IsNullOrEmpty(base.TargetFramework))
            {
                Microsoft.VisualStudio.Shell.Interop.IVsStringMap map;
                Microsoft.VisualStudio.Shell.Interop.IVsDataObjectStringMapManager manager = provider.GetService(typeof(Microsoft.VisualStudio.Shell.Interop.SVsDataObjectStringMapManager)) as Microsoft.VisualStudio.Shell.Interop.IVsDataObjectStringMapManager;
                manager.CreateStringMap(out map);
                map.SetValue("TypeName", szValue);
                map.SetValue("AssemblyName", base.AssemblyName.FullName);
                map.SetValue("Frameworks", base.TargetFramework);
                map.SetValue("ItemProvider", "{" + base.GetType().GUID.ToString() + "}");
                manager.WriteStringMap(dataObj, "MultiTargeting:{FBB22D27-7B21-42ac-88C8-595F94BDBCA5}", 1, map);
            }
            object service = provider.GetService(typeof(SVsToolbox));
            IVsToolbox2 toolbox = service as IVsToolbox2;
            IVsToolbox3 toolbox2 = service as IVsToolbox3;
            if (string.IsNullOrEmpty(tabName))
            {
                if (base.ToolboxItemCategories.ContainsKey(szValue))
                {
                    tabName = base.ToolboxItemCategories[szValue];
                }
                else
                {
                    tabName = base.DefaultCategory;
                }
            }
            int hr = 0;
            if (!string.IsNullOrEmpty(tabName))
            {
                if ((string.CompareOrdinal(tabName, "Common") == 0) && !ErrorHandler.Succeeded(toolbox2.GetTabOfID(this.InvariantCommonCategory, out tabName)))
                {
                    tabName = null;
                }
                if (!string.IsNullOrEmpty(tabName))
                {
                    TBXITEMINFO[] ptif = new TBXITEMINFO[] { itemInfo };
                    hr = toolbox.AddItem(dataObj, ptif, tabName);
                }
            }
            if (!string.IsNullOrEmpty(this.InvariantAllItemsCategory))
            {
                if (!ErrorHandler.Succeeded(toolbox2.GetTabOfID(this.InvariantAllItemsCategory, out tabName)))
                {
                    tabName = null;
                }
                if (!string.IsNullOrEmpty(tabName))
                {
                    TBXITEMINFO[] tbxiteminfoArray2 = new TBXITEMINFO[] { itemInfoAll };
                    hr = toolbox.AddItem(dataObj, tbxiteminfoArray2, tabName);
                }
            }
            if (ErrorHandler.Succeeded(hr))
            {
                base.RegisteredControls.Add(control);
                return true;
            }
            return false;
        }

        protected abstract DictionaryControl CreateControl(string fullTypeName, IDictionary toolProperties);
        private IsolationProvider GetIsolationProvider(System.IServiceProvider provider)
        {
            FrameworkName targetFramework = new FrameworkName(this.GetTargetIsolationFramework());
            if ((targetFramework != _lastTargetFrameworkName) || (_lastIsolationProvider == null))
            {
                IsolationProviderService service = provider.GetService(typeof(IsolationProviderService)) as IsolationProviderService;
                if (service == null)
                {
                    throw new InvalidOperationException();
                }
                UnloadAppDomain();
                _lastIsolationProvider = service.CreateGlobalIsolationProvider(targetFramework);
                _lastTargetFrameworkName = targetFramework;
            }
            return _lastIsolationProvider;
        }

        private string GetTargetIsolationFramework()
        {
            return (base.TargetFramework ?? this.DefaultFramework);
        }

        public static void UnloadAppDomain()
        {
            if (_lastIsolationProvider != null)
            {
                _lastIsolationProvider.Dispose();
                _lastIsolationProvider = null;
            }
            _lastTargetFrameworkName = null;
        }

        protected abstract int ClipFormat { get; }

        protected abstract string DefaultFramework { get; }

        protected abstract List<string> ExclusionList { get; }

        protected abstract AssemblyName FrameworkElementAssemblySpec { get; }

        protected abstract string InvariantAllItemsCategory { get; }

        protected abstract string InvariantCommonCategory { get; }

        [ComVisible(true)]
        private class IsolationProxy : ICustomQueryInterface, IQueryToolboxItems
        {
            private IModifiableMetadataContext _metadataContext = (AppDomain.CurrentDomain.GetData(typeof(IMetadataContext).FullName) as IModifiableMetadataContext);
            private static Guid IID_IManagedObject = new Guid("{C3FCC19E-A970-11D2-8B5A-00A0C9B7C9C4}");

            //internal CreationTool GetCreationTool(ITypeMetadata controlTypeMeta)
            //{
            //    ITypeMetadata attributeType = this._metadataContext.GetType(typeof(CreationToolAttribute));
            //    System.Type runtimeType = controlTypeMeta.GetRuntimeType();
            //    CreationTool tool = null;
            //    foreach (IAttributeMetadata metadata2 in controlTypeMeta.GetAttributes(attributeType))
            //    {
            //        CreationToolAttribute attribute = (CreationToolAttribute) metadata2.GetValue();
            //        if (attribute.ToolType != null)
            //        {
            //            tool = Activator.CreateInstance(attribute.ToolType) as CreationTool;
            //        }
            //        if (tool != null)
            //        {
            //            tool.CreationType = runtimeType;
            //        }
            //        return tool;
            //    }
            //    return tool;
            //}

            public CustomQueryInterfaceResult GetInterface(ref Guid iid, out IntPtr ppv)
            {
                ppv = IntPtr.Zero;
                if (iid.Equals(IID_IManagedObject))
                {
                    return CustomQueryInterfaceResult.Failed;
                }
                return CustomQueryInterfaceResult.NotHandled;
            }

            public ICollection GetToolboxItems(AssemblyName assemblyName, AssemblyName feElementAssemblySpec, IList exclusionList)
            {
                if (this._metadataContext == null)
                {
                    return null;
                }
                ArrayList list = new ArrayList();
                try
                {
                    this._metadataContext.AssemblyNames.Add(assemblyName);
                    IAssemblyMetadata assembly = this._metadataContext.GetAssembly(assemblyName.FullName);
                    if (assembly == null)
                    {
                        return list;
                    }
                    IAssemblyMetadata metadata2 = null;
                    foreach (IAssemblyMetadata metadata3 in this._metadataContext.Assemblies)
                    {
                        if (metadata3.Name.Equals(feElementAssemblySpec.Name, StringComparison.Ordinal))
                        {
                            metadata2 = metadata3;
                            break;
                        }
                    }
                    if (metadata2 == null)
                    {
                        return null;
                    }
                    ITypeMetadata type = metadata2.GetType(typeof(FrameworkElement).FullName);
                    if (type == null)
                    {
                        return null;
                    }
                    foreach (ITypeMetadata metadata5 in assembly.Types)
                    {
                        if (((!exclusionList.Contains(metadata5.FullName) && type.IsAssignableFrom(metadata5)) && (!metadata5.IsAbstract && metadata5.IsVisible)) /* && !this.HiddenFromToolbox(metadata5) */)
                        {
                            //CreationTool creationTool = this.GetCreationTool(metadata5);
                            //if (creationTool != null)
                            //{
                            //    IDictionary toolProperties = ToolEncoder.GetToolProperties(creationTool);
                            //    toolProperties.Add("VSTOOLBOXTAB", this.GetToolboxTab(metadata5));
                            //    list.Add(toolProperties);
                            //}
                        }
                    }
                }
                catch (Exception exception)
                {
                    ReflectionTypeLoadException exception2 = exception as ReflectionTypeLoadException;
                    if (exception2 != null)
                    {
                        throw new ReflectionTypeLoadException(null, exception2.LoaderExceptions, exception2.Message);
                    }
                    throw;
                }
                return list;
            }

            //internal string GetToolboxTab(ITypeMetadata controlTypeMeta)
            //{
            //    ITypeMetadata type = this._metadataContext.GetType(typeof(ToolboxTabNameAttribute));
            //    foreach (IAttributeMetadata metadata2 in controlTypeMeta.GetAttributes(type))
            //    {
            //        ToolboxTabNameAttribute attribute = (ToolboxTabNameAttribute) metadata2.GetValue();
            //        return attribute.TabName;
            //    }
            //    return null;
            //}

            //internal bool HiddenFromToolbox(ITypeMetadata controlTypeMeta)
            //{
            //    ITypeMetadata type = this._metadataContext.GetType(typeof(ToolboxBrowsableAttribute));
            //    foreach (IAttributeMetadata metadata2 in controlTypeMeta.GetAttributes(type))
            //    {
            //        ToolboxBrowsableAttribute attribute = (ToolboxBrowsableAttribute) metadata2.GetValue();
            //        return !attribute.Browsable;
            //    }
            //    return false;
            //}
        }
    }
}
Back to Top