internal static void AnalyzePSSnapInAssembly( Assembly assembly, string name, PSSnapInInfo psSnapInInfo, out Dictionary <string, SessionStateCmdletEntry> cmdlets, out Dictionary <string, SessionStateProviderEntry> providers) { if (assembly == null) { throw new ArgumentNullException(nameof(assembly)); } if (PSSnapInHelpers._cmdletCache != null && PSSnapInHelpers._providerCache != null && (PSSnapInHelpers._cmdletCache.ContainsKey(assembly) && PSSnapInHelpers._providerCache.ContainsKey(assembly))) { cmdlets = PSSnapInHelpers._cmdletCache[assembly]; providers = PSSnapInHelpers._providerCache[assembly]; } else { cmdlets = (Dictionary <string, SessionStateCmdletEntry>)null; providers = (Dictionary <string, SessionStateProviderEntry>)null; PSSnapInHelpers._PSSnapInTracer.WriteLine("Analyzing assembly {0} for cmdlet and providers", (object)assembly.Location); string helpFile = PSSnapInHelpers.GetHelpFile(assembly.Location); Type[] types; try { types = assembly.GetTypes(); } catch (ReflectionTypeLoadException ex) { string str = ex.Message + "\nLoader Exceptions: \n"; if (ex.LoaderExceptions != null) { foreach (Exception loaderException in ex.LoaderExceptions) { str = str + "\n" + loaderException.Message; } } PSSnapInHelpers._PSSnapInTracer.TraceError(str); throw new PSSnapInException(name, str); } foreach (Type type in types) { if ((type.IsPublic || type.IsNestedPublic) && !type.IsAbstract) { object[] customAttributes = type.GetCustomAttributes(false); string str1 = (string)null; string str2 = (string)null; foreach (object obj in customAttributes) { if (obj.GetType() == typeof(CmdletAttribute)) { str1 = PSSnapInHelpers.GetCmdletName(obj as CmdletAttribute); break; } if (obj.GetType() == typeof(CmdletProviderAttribute)) { str2 = PSSnapInHelpers.GetProviderName(obj as CmdletProviderAttribute); break; } } if (!string.IsNullOrEmpty(str1)) { if (PSSnapInHelpers.IsCmdletClass(type) && PSSnapInHelpers.HasDefaultConstructor(type)) { if (cmdlets != null && cmdlets.ContainsKey(str1)) { string str3 = ResourceManagerCache.FormatResourceString("ConsoleInfoErrorStrings", "PSSnapInDuplicateCmdlets", (object)str1, (object)name); PSSnapInHelpers._PSSnapInTracer.TraceError(str3); throw new PSSnapInException(name, str3); } SessionStateCmdletEntry stateCmdletEntry = new SessionStateCmdletEntry(str1, type, helpFile); if (psSnapInInfo != null) { stateCmdletEntry.SetPSSnapIn(psSnapInInfo); } if (cmdlets == null) { cmdlets = new Dictionary <string, SessionStateCmdletEntry>((IEqualityComparer <string>)StringComparer.OrdinalIgnoreCase); } cmdlets.Add(str1, stateCmdletEntry); PSSnapInHelpers._PSSnapInTracer.WriteLine("{0} from type {1} is added as a cmdlet. ", (object)str1, (object)type.FullName); continue; } PSSnapInHelpers._PSSnapInTracer.TraceWarning("{0} is not valid cmdlet because it doesn't derive from the Cmdlet type or it doesn't have a default constructor.", (object)str1); } if (!string.IsNullOrEmpty(str2)) { if (PSSnapInHelpers.IsProviderClass(type) && PSSnapInHelpers.HasDefaultConstructor(type)) { if (providers != null && providers.ContainsKey(str2)) { string str3 = ResourceManagerCache.FormatResourceString("ConsoleInfoErrorStrings", "PSSnapInDuplicateProviders", (object)str2, (object)psSnapInInfo.Name); PSSnapInHelpers._PSSnapInTracer.TraceError(str3); throw new PSSnapInException(psSnapInInfo.Name, str3); } SessionStateProviderEntry stateProviderEntry = new SessionStateProviderEntry(str2, type, helpFile); stateProviderEntry.SetPSSnapIn(psSnapInInfo); if (providers == null) { providers = new Dictionary <string, SessionStateProviderEntry>((IEqualityComparer <string>)StringComparer.OrdinalIgnoreCase); } providers.Add(str2, stateProviderEntry); PSSnapInHelpers._PSSnapInTracer.WriteLine("{0} from type {1} is added as a provider. ", (object)str2, (object)type.FullName); } else { PSSnapInHelpers._PSSnapInTracer.TraceWarning("{0} is not valid provider because it doesn't derive from the provider type or it doesn't have a default constructor.", (object)str2); } } } } lock (PSSnapInHelpers._syncObject) { if (cmdlets != null) { if (PSSnapInHelpers._cmdletCache == null) { PSSnapInHelpers._cmdletCache = new Dictionary <Assembly, Dictionary <string, SessionStateCmdletEntry> >(); } PSSnapInHelpers._cmdletCache[assembly] = cmdlets; } if (providers == null) { return; } if (PSSnapInHelpers._providerCache == null) { PSSnapInHelpers._providerCache = new Dictionary <Assembly, Dictionary <string, SessionStateProviderEntry> >(); } PSSnapInHelpers._providerCache[assembly] = providers; } } }
private void LoadPSSnapIn(PSSnapInInfo mshsnapinInfo) { if (mshsnapinInfo == null) { return; } #if !CORECLR // CustomPSSnapIn Not Supported On CSS. if (!String.IsNullOrEmpty(mshsnapinInfo.CustomPSSnapInType)) { LoadCustomPSSnapIn(mshsnapinInfo); return; } #endif Assembly assembly = null; s_mshsnapinTracer.WriteLine("Loading assembly for mshsnapin {0}", mshsnapinInfo.Name); assembly = LoadMshSnapinAssembly(mshsnapinInfo); if (assembly == null) { s_mshsnapinTracer.TraceError("Loading assembly for mshsnapin {0} failed", mshsnapinInfo.Name); return; } s_mshsnapinTracer.WriteLine("Loading assembly for mshsnapin {0} succeeded", mshsnapinInfo.Name); Dictionary <string, SessionStateCmdletEntry> cmdlets = null; Dictionary <string, List <SessionStateAliasEntry> > aliases = null; Dictionary <string, SessionStateProviderEntry> providers = null; string throwAwayHelpFile = null; PSSnapInHelpers.AnalyzePSSnapInAssembly(assembly, assembly.Location, mshsnapinInfo, null, false, out cmdlets, out aliases, out providers, out throwAwayHelpFile); if (cmdlets != null) { foreach (var c in cmdlets) { CmdletConfigurationEntry cmdlet = new CmdletConfigurationEntry(c.Key, c.Value.ImplementingType, c.Value.HelpFileName, mshsnapinInfo); _cmdlets.AddBuiltInItem(cmdlet); } } if (providers != null) { foreach (var p in providers) { ProviderConfigurationEntry provider = new ProviderConfigurationEntry(p.Key, p.Value.ImplementingType, p.Value.HelpFileName, mshsnapinInfo); _providers.AddBuiltInItem(provider); } } foreach (string file in mshsnapinInfo.Types) { string path = Path.Combine(mshsnapinInfo.ApplicationBase, file); TypeConfigurationEntry typeEntry = new TypeConfigurationEntry(path, path, mshsnapinInfo); this.Types.AddBuiltInItem(typeEntry); } foreach (string file in mshsnapinInfo.Formats) { string path = Path.Combine(mshsnapinInfo.ApplicationBase, file); FormatConfigurationEntry formatEntry = new FormatConfigurationEntry(path, path, mshsnapinInfo); this.Formats.AddBuiltInItem(formatEntry); } AssemblyConfigurationEntry assemblyEntry = new AssemblyConfigurationEntry(mshsnapinInfo.AssemblyName, mshsnapinInfo.AbsoluteModulePath, mshsnapinInfo); this.Assemblies.AddBuiltInItem(assemblyEntry); return; }