public static ProviderCapabilities GetCapabilities(Type type) { try { object[] customAttributes = type.GetCustomAttributes(typeof(CmdletProviderAttribute), false); if ((customAttributes != null) && (customAttributes.Length == 1)) { CmdletProviderAttribute attribute = (CmdletProviderAttribute)customAttributes[0]; return(attribute.ProviderCapabilities); } } catch { // TODO: what if the provider has no capabilities? } return(ProviderCapabilities.None); }
private static string GetProviderName(CmdletProviderAttribute providerAttribute) => providerAttribute.ProviderName;
private static string GetProviderName(CmdletProviderAttribute providerAttribute) { return(providerAttribute.ProviderName); }
internal static void AnalyzePSSnapInAssembly(Assembly assembly, string name, PSSnapInInfo psSnapInInfo, PSModuleInfo moduleInfo, bool isModuleLoad, out Dictionary <string, SessionStateCmdletEntry> cmdlets, out Dictionary <string, SessionStateProviderEntry> providers, out string helpFile) { Type[] assemblyTypes; helpFile = null; if (assembly == null) { throw new ArgumentNullException("assembly"); } cmdlets = null; providers = null; if (_cmdletCache.Value.ContainsKey(assembly)) { cmdlets = new Dictionary <string, SessionStateCmdletEntry>(_cmdletCache.Value.Count, StringComparer.OrdinalIgnoreCase); Dictionary <string, SessionStateCmdletEntry> dictionary = _cmdletCache.Value[assembly]; foreach (string str in dictionary.Keys) { SessionStateCmdletEntry entry = dictionary[str]; if ((entry.PSSnapIn == null) && (psSnapInInfo != null)) { entry.SetPSSnapIn(psSnapInInfo); } SessionStateCmdletEntry entry2 = (SessionStateCmdletEntry)entry.Clone(); cmdlets[str] = entry2; } } if (_providerCache.Value.ContainsKey(assembly)) { providers = new Dictionary <string, SessionStateProviderEntry>(_providerCache.Value.Count, StringComparer.OrdinalIgnoreCase); Dictionary <string, SessionStateProviderEntry> dictionary2 = _providerCache.Value[assembly]; foreach (string str2 in dictionary2.Keys) { SessionStateProviderEntry entry3 = dictionary2[str2]; if ((entry3.PSSnapIn == null) && (psSnapInInfo != null)) { entry3.SetPSSnapIn(psSnapInInfo); } SessionStateProviderEntry entry4 = (SessionStateProviderEntry)entry3.Clone(); providers[str2] = entry4; } } if ((cmdlets != null) || (providers != null)) { if (!_assembliesWithModuleInitializerCache.Value.ContainsKey(assembly)) { _PSSnapInTracer.WriteLine("Returning cached cmdlet and provider entries for {0}", new object[] { assembly.Location }); } else { _PSSnapInTracer.WriteLine("Executing IModuleAssemblyInitializer.Import for {0}", new object[] { assembly.Location }); assemblyTypes = GetAssemblyTypes(assembly, name); ExecuteModuleInitializer(assembly, assemblyTypes, isModuleLoad); } } else { _PSSnapInTracer.WriteLine("Analyzing assembly {0} for cmdlet and providers", new object[] { assembly.Location }); helpFile = GetHelpFile(assembly.Location); assemblyTypes = GetAssemblyTypes(assembly, name); ExecuteModuleInitializer(assembly, assemblyTypes, isModuleLoad); Type type = null; Type type2 = null; foreach (Type type3 in assemblyTypes) { if ((type3.IsPublic || type3.IsNestedPublic) && !type3.IsAbstract) { if (IsCmdletClass(type3) && HasDefaultConstructor(type3)) { type = type3; CmdletAttribute customAttribute = GetCustomAttribute <CmdletAttribute>(type3); if (customAttribute != null) { string cmdletName = GetCmdletName(customAttribute); if (!string.IsNullOrEmpty(cmdletName)) { if ((cmdlets != null) && cmdlets.ContainsKey(cmdletName)) { string errorMessageFormat = StringUtil.Format(ConsoleInfoErrorStrings.PSSnapInDuplicateCmdlets, cmdletName, name); _PSSnapInTracer.TraceError(errorMessageFormat, new object[0]); throw new PSSnapInException(name, errorMessageFormat); } SessionStateCmdletEntry entry5 = new SessionStateCmdletEntry(cmdletName, type3, helpFile); if (psSnapInInfo != null) { entry5.SetPSSnapIn(psSnapInInfo); } if (cmdlets == null) { cmdlets = new Dictionary <string, SessionStateCmdletEntry>(StringComparer.OrdinalIgnoreCase); } cmdlets.Add(cmdletName, entry5); _PSSnapInTracer.WriteLine("{0} from type {1} is added as a cmdlet. ", new object[] { cmdletName, type3.FullName }); } } } else if (IsProviderClass(type3) && HasDefaultConstructor(type3)) { type2 = type3; CmdletProviderAttribute providerAttribute = GetCustomAttribute <CmdletProviderAttribute>(type3); if (providerAttribute != null) { string providerName = GetProviderName(providerAttribute); if (!string.IsNullOrEmpty(providerName)) { if ((providers != null) && providers.ContainsKey(providerName)) { string str6 = StringUtil.Format(ConsoleInfoErrorStrings.PSSnapInDuplicateProviders, providerName, psSnapInInfo.Name); _PSSnapInTracer.TraceError(str6, new object[0]); throw new PSSnapInException(psSnapInInfo.Name, str6); } SessionStateProviderEntry entry6 = new SessionStateProviderEntry(providerName, type3, helpFile); entry6.SetPSSnapIn(psSnapInInfo); if (moduleInfo != null) { entry6.SetModule(moduleInfo); } if (providers == null) { providers = new Dictionary <string, SessionStateProviderEntry>(StringComparer.OrdinalIgnoreCase); } providers.Add(providerName, entry6); _PSSnapInTracer.WriteLine("{0} from type {1} is added as a provider. ", new object[] { providerName, type3.FullName }); } } } } } if (((providers == null) || (providers.Count == 0)) && ((cmdlets == null) || (cmdlets.Count == 0))) { try { if (type != null) { ConstructorInfo constructor = type.GetConstructor(Type.EmptyTypes); if (constructor != null) { constructor.Invoke(null); } } if (type2 != null) { ConstructorInfo info2 = type2.GetConstructor(Type.EmptyTypes); if (info2 != null) { info2.Invoke(null); } } } catch (TargetInvocationException exception) { throw exception.InnerException; } } if (cmdlets != null) { Dictionary <string, SessionStateCmdletEntry> dictionary3 = new Dictionary <string, SessionStateCmdletEntry>(cmdlets.Count, StringComparer.OrdinalIgnoreCase); foreach (KeyValuePair <string, SessionStateCmdletEntry> pair in cmdlets) { dictionary3[pair.Key] = (SessionStateCmdletEntry)pair.Value.Clone(); } _cmdletCache.Value[assembly] = dictionary3; } if (providers != null) { Dictionary <string, SessionStateProviderEntry> dictionary4 = new Dictionary <string, SessionStateProviderEntry>(providers.Count, StringComparer.OrdinalIgnoreCase); foreach (KeyValuePair <string, SessionStateProviderEntry> pair2 in providers) { dictionary4[pair2.Key] = (SessionStateProviderEntry)pair2.Value.Clone(); } _providerCache.Value[assembly] = providers; } } }