예제 #1
0
 public override InitialSessionStateEntry Clone()
 {
     SessionStateProviderEntry entry = new SessionStateProviderEntry(base.Name, this._implementingType, this._helpFileName, base.Visibility);
     entry.SetPSSnapIn(base.PSSnapIn);
     entry.SetModule(base.Module);
     return entry;
 }
예제 #2
0
        public override InitialSessionStateEntry Clone()
        {
            SessionStateProviderEntry entry = new SessionStateProviderEntry(base.Name, this._implementingType, this._helpFileName, base.Visibility);

            entry.SetPSSnapIn(base.PSSnapIn);
            entry.SetModule(base.Module);
            return(entry);
        }
예제 #3
0
		private ProviderInfo GetProvider()
        {
            ExecutionContext executionContext = GetExecutionContext();
            SessionStateInternal sessionState = new SessionStateInternal(executionContext);
			
			SessionStateProviderEntry providerEntry = new SessionStateProviderEntry("FileSystem",typeof(FileSystemProvider), null);
			sessionState.AddSessionStateEntry(providerEntry);
            ProviderInfo matchingProvider = sessionState.ProviderList.ToList()[0];

            return matchingProvider;
        }
예제 #4
0
 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;
         }
     }
 }
예제 #5
0
 /// <summary>
 /// Entrypoint used by to add a provider to the current session state
 /// based on a SessionStateProviderEntry.
 /// </summary>
 /// <param name="providerEntry"></param>
 internal void AddSessionStateEntry(SessionStateProviderEntry providerEntry)
 {
     ProviderInfo provider = AddProvider(providerEntry.ImplementingType,
                     providerEntry.Name,
                     providerEntry.HelpFileName,
                     providerEntry.PSSnapIn,
                     providerEntry.Module
     );
 }
예제 #6
0
 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;
         }
     }
 }