// TODO: separate providers from cmdlets internal Collection <CmdletInfo> LoadCmdletsFromPSSnapin(string strType, out Collection <SnapinProviderPair> providers) { Type snapinType = Type.GetType(strType, true); Assembly assembly = snapinType.Assembly; PSSnapIn snapin = Activator.CreateInstance(snapinType) as PSSnapIn; PSSnapInInfo snapinInfo = new PSSnapInInfo(snapin.Name, false, string.Empty, assembly.GetName().Name, string.Empty, new Version(1, 0), null, null, null, snapin.Description, snapin.Vendor); var snapinProviderPairs = from Type type in assembly.GetTypes() where !type.IsSubclassOf(typeof(Cmdlet)) where type.IsSubclassOf(typeof(CmdletProvider)) from CmdletProviderAttribute providerAttr in type.GetCustomAttributes(typeof(CmdletProviderAttribute), true) select new SnapinProviderPair { snapinInfo = snapinInfo, providerType = type, providerAttr = providerAttr }; providers = snapinProviderPairs.ToCollection(); var cmdletInfos = from Type type in assembly.GetTypes() where type.IsSubclassOf(typeof(Cmdlet)) from CmdletAttribute cmdletAttribute in type.GetCustomAttributes(typeof(CmdletAttribute), true) select new CmdletInfo(cmdletAttribute.FullName, type, null, snapinInfo, _context); return(cmdletInfos.ToCollection()); }
/// <summary> /// Adds the PSSnapIn by name/path /// </summary> /// <exception cref="PSArgumentException">If loading the snapin fails (e.g. invalid name/path)</exception> /// <param name="name">Either the name of the registered snapin or a path to an assembly</param> /// <returns>The newly added PSSnapIn</returns> internal PSSnapInInfo AddPSSnapIn(string name, ExecutionContext context) { // a slash is not part of a valid snapin name. If the name contains a slash (e.g. "./foobar") its likely // that the user wants to load an assembly directly Assembly assembly = null; if (name.Contains(PathIntrinsics.CorrectSlash) || name.Contains(PathIntrinsics.WrongSlash)) { assembly = LoadAssemblyFromFile(name); } else { assembly = LoadRegisteredPSSnapInAssembly(name); } //load snapins from assembly and make sure it's only one snapin class defined in thwere var snapins = from Type type in assembly.GetTypes() where type.IsSubclassOf(typeof(PSSnapIn)) select type; if (snapins.Count() != 1) { string errorMsg = "The assembly '{0}' contains either no or more than one PSSnapIn class!"; throw new PSSnapInException(String.Format(errorMsg, assembly.FullName)); } PSSnapIn snapin = (PSSnapIn)Activator.CreateInstance(snapins.First()); //okay, we got the new snapin. now load it PSSnapInInfo snapinInfo = new PSSnapInInfo(snapin, assembly, false); LoadPSSnapIn(snapinInfo, assembly, context); return(snapinInfo); }
internal void LoadDefaultPSSnapIns() { foreach (var defaultSnapin in _defaultSnapins) { Type snapinType = Type.GetType(defaultSnapin, true); PSSnapIn snapin = Activator.CreateInstance(snapinType) as PSSnapIn; Assembly snapinAssembly = snapinType.Assembly; LoadPSSnapIn(new PSSnapInInfo(snapin, snapinAssembly, true), snapinAssembly); } }
// TODO: separate providers from cmdlets internal Collection <CmdletInfo> LoadCmdletsFromPSSnapin(string strType, out Collection <SnapinProviderPair> providers) { Collection <CmdletInfo> collection = new Collection <CmdletInfo>(); providers = new Collection <SnapinProviderPair>(); try { Type snapinType = Type.GetType(strType, true); Assembly assembly = snapinType.Assembly; PSSnapIn snapin = Activator.CreateInstance(snapinType) as PSSnapIn; PSSnapInInfo snapinInfo = new PSSnapInInfo(snapin.Name, false, string.Empty, assembly.GetName().Name, string.Empty, new Version(1, 0), null, null, null, snapin.Description, snapin.Vendor); foreach (Type type in assembly.GetTypes()) { if (type.IsSubclassOf(typeof(Cmdlet))) { foreach (CmdletAttribute cmdletAttribute in type.GetCustomAttributes(typeof(CmdletAttribute), true)) { CmdletInfo cmdletInfo = new CmdletInfo(cmdletAttribute.ToString(), type, null, snapinInfo, _context); collection.Add(cmdletInfo); } continue; } if (type.IsSubclassOf(typeof(CmdletProvider))) { foreach (CmdletProviderAttribute providerAttr in type.GetCustomAttributes(typeof(CmdletProviderAttribute), true)) { providers.Add(new SnapinProviderPair() { snapinInfo = snapinInfo, providerType = type, providerAttr = providerAttr }); } continue; } } } catch (Exception ex) { System.Diagnostics.Trace.WriteLine(ex.ToString()); } return(collection); }
internal void LoadDefaultPSSnapIns() { foreach (var snapinPair in _defaultSnapins) { var defaultSnapin = snapinPair.Key; var required = snapinPair.Value; Type snapinType = Type.GetType(defaultSnapin, false); if (snapinType == null) { if (!required) { continue; } throw new TypeLoadException("Couldn't load type for required default snapin '" + defaultSnapin + "'"); } PSSnapIn snapin = Activator.CreateInstance(snapinType) as PSSnapIn; Assembly snapinAssembly = snapinType.Assembly; LoadPSSnapIn(new PSSnapInInfo(snapin, snapinAssembly, true), snapinAssembly, _globalExecutionContext); } }