Partilhar via


.NET types

In response to the recent Days till Xmas post, applepwc asked the question 
   >  where can I find more ".NET types"?I mean is there a list of ".NET type"  available in monad?

Excellent question but there are a number of aspects to it so let's break it down:

.NET is a developer platform.  That platform contains a number of libraries that developers can use to develope their code.  Reference information for the classes that ship with the .NET platform are available at https://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatetimememberstopic.asp .

Other developers take that platform and produce their own .NET types and ship them as assemblies (DLLs) or included in their executables.  Each team will provide they own documentation for their types.  Monad makes this information avilable as part of its SDK.

Then there is the question: which of these types are available to Monad.  The answer is that Monad provides access to any public .NET type which is loaded in process.  The following sequence tells you what assemblies you have loaded in your current process (appdomain actually but we don't need to go there now).

MSH> [appdomain]::currentdomain.getassemblies() |ft fullname

FullName
--------
mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5...
System.Management.Automation.ConsoleHost, Version=1.0.490.0, Cul...
System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c5...
System.Management.Automation, Version=1.0.490.0, Culture=neutral...
System.Management.Automation.ConsoleHost.resources, Version=1.0....
System.Configuration.Install, Version=2.0.0.0, Culture=neutral, ...
System.Management.Automation.Commands.Management, Version=1.0.49...
System.Management.Automation.Security, Version=1.0.490.0, Cultur...
System.Management.Automation.Commands.Utility, Version=1.0.490.0...
System.Management, Version=2.0.0.0, Culture=neutral, PublicKeyTo...
System.DirectoryServices, Version=2.0.0.0, Culture=neutral, Publ...
System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b7...
System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77...
System.Management.Automation.resources, Version=1.0.490.0, Cultu...
System.Management.Automation.Security.resources, Version=1.0.490...
System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKe...
System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken...
System.Management.Automation.Commands.Utility.resources, Version...
System.Management.Automation.Commands.Management.resources, Vers...

This call returns an assembly object.  This object has a method GetExportedTypes() which gets all the public types (as opposed to private types which you don't have access to) for that assemblies.  Thus if you wanted to get all the types available in process you would do the following: 

MSH> [appdomain]::currentdomain.getassemblies() |foreach {$_.getexpo
rtedtypes()}

IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object
True False ICloneable
True False IEnumerable
True False ICollection
True False IList
True True Array Syste...
....

I use this quite a bit actually.  I create a function Match-Type to find a set of .Net Types:

function Match-Type ($Name)
{ [Appdomain]::CurrentDomain.GetAssemblies() |
foreach {$_.GetExportedTypes()} |
where {$_.FullName -match $Name}
}

MSH> Match-Type mshhost |ft FullName

FullName
--------
Microsoft.Management.Automation.MshHostMshSnapIn
System.Management.Automation.Host.MshHost
System.Management.Automation.Host.MshHostRawUserInterface
System.Management.Automation.Host.MshHostUserInterface

This is a good point to take a tangent. In an earlier post, I pointe dou that some .NET types can parse strings and how wonderful that was. You might reasonably ask yourself the question: Which types support the Parse() method. Here is a function that helps address that:

function Match-Method ($Name)
{ [Appdomain]::CurrentDomain.GetAssemblies() |
foreach {$_.GetExportedTypes()} |
foreach {
$Type = $_
if ($Type.GetMethods() | where {$_.Name -match $Name}) {
$Type
}
}
}
MSH> match-Method Parse

IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Enum Syste...
True True DateTime Syste...
True True Boolean Syste...
True True Byte Syste...
True True Char Syste...
...

The last issue is, how do I load types into my process if they aren't already there. Lee Holmes has a great blog entry on that at: https://www.leeholmes.com/blog/HowDoIEasilyLoadAssembliesWhenLoadWithPartialNameHasBeenDeprecated.aspx

I hope that this was useful.

Enjoy!

Jeffrey P. Snover
Monad Architect

Comments

  • Anonymous
    February 08, 2006
    Great post.
    Thanks very much!
  • Anonymous
    February 09, 2006
    > I use this quite a bit actually.  I create a function Match-Type

    I think this kind of blog post which gives people pointers on what can be done is really great and fun to read.

    With these insights in mind, we could be more adventurous.  Oh yeah, while at it, why don't you give us certain problems for us to work with as well?  :)
  • Anonymous
    February 28, 2006
    Don't you think that "foreach" is more like "select"?And it would be helpful to exchange semantics between both,because most of the time under "foreach" you mean "select" and current "select" semantics is more confusing than "foreach".

    Thanks.