|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Get type from string.I am trying to do some reflection and it seems that Type.GetType(string) fits
what I want to do. There is only one problem is that the string requires an assembly qualified name in order to get the type. I would like to just supply the first two parts or the assembly qualified name (namespace+type, assembly path) as the following portions (Version, PublicToken, Culture) are likely to change. Is there a way to "automatically" get the assembly qualified name? Say I am interested in the SqlConnection type but Micosoft updates the version. My reflected type will be on the only type not the updated type. In Visual Studio you specify a reference based on the namespace and you don't specify the PublicToken and Version etc. I would like my reflection to work somewhat like that. Any ideas? I would be willing to search through the GAC if there is a relatively easy API to do so. Thank you. Kevin Burton Hi Kevin,
If you know the path to the assembly you can do this: Assembly assem = Assembly.LoadFile("PathToTheAssembly.dll"); Type t = assem.GetType("Namespace.And.Type.Name"); That will give you the type from the assembly, you can then use the Activator object to create an instance of the type. -David Sandor Show quote "Kevin Burton" wrote: > I am trying to do some reflection and it seems that Type.GetType(string) fits > what I want to do. There is only one problem is that the string requires an > assembly qualified name in order to get the type. I would like to just supply > the first two parts or the assembly qualified name (namespace+type, assembly > path) as the following portions (Version, PublicToken, Culture) are likely to > change. Is there a way to "automatically" get the assembly qualified name? > Say I am interested in the SqlConnection type but Micosoft updates the > version. My reflected type will be on the only type not the updated type. In > Visual Studio you specify a reference based on the namespace and you don't > specify the PublicToken and Version etc. I would like my reflection to work > somewhat like that. Any ideas? I would be willing to search through the GAC > if there is a relatively easy API to do so. Thank you. > > Kevin Burton Kevin,
>I am trying to do some reflection and it seems that Type.GetType(string) fits What you're asking for is called a partial bind and can be done with>what I want to do. There is only one problem is that the string requires an >assembly qualified name in order to get the type. I would like to just supply >the first two parts or the assembly qualified name (namespace+type, assembly >path) as the following portions (Version, PublicToken, Culture) are likely to >change. Is there a way to "automatically" get the assembly qualified name? Assembly.LoadWithPartialName. But that API has been deprecated. See the reasons here http://blogs.msdn.com/suzcook/archive/2003/05/30/57159.aspx >Say I am interested in the SqlConnection type but Micosoft updates the There are binding redirects in place for the system assemblies. So>version. even if you specify an earlier version, you'll get the version that matches the framework version you're running under. >In Visual Studio you specify a reference based on the namespace and you don't Namespaces have nothing to do with assembly names.>specify the PublicToken and Version etc. Mattias -- Mattias Sjögren [C# MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com Please reply only to the newsgroup. Rather the loading from a specific assembly using a Path, if I know ahead of
time that my assembly will be loaded in the GAC is there a way to instantiate an Assembly class from some information in the GAC? Similar to Visual Studio when you add a reference there are several tabs one being the "System" or ..NET types. Show quote "Mattias Sjögren" wrote: > Kevin, > > >I am trying to do some reflection and it seems that Type.GetType(string) fits > >what I want to do. There is only one problem is that the string requires an > >assembly qualified name in order to get the type. I would like to just supply > >the first two parts or the assembly qualified name (namespace+type, assembly > >path) as the following portions (Version, PublicToken, Culture) are likely to > >change. Is there a way to "automatically" get the assembly qualified name? > > What you're asking for is called a partial bind and can be done with > Assembly.LoadWithPartialName. But that API has been deprecated. See > the reasons here > http://blogs.msdn.com/suzcook/archive/2003/05/30/57159.aspx > > > >Say I am interested in the SqlConnection type but Micosoft updates the > >version. > > There are binding redirects in place for the system assemblies. So > even if you specify an earlier version, you'll get the version that > matches the framework version you're running under. > > > >In Visual Studio you specify a reference based on the namespace and you don't > >specify the PublicToken and Version etc. > > Namespaces have nothing to do with assembly names. > > > Mattias > > -- > Mattias Sjögren [C# MVP] mattias @ mvps.org > http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com > Please reply only to the newsgroup. > Kevin,
>Similar to Visual Studio That tab lists assemblies in the framework directory,>when you add a reference there are several tabs one being the "System" or >.NET types. %WINDIR%\Microsoft.NET\Framework\vX.Y.ZZZZ (plus some additional directories, see KB article 306149 for details). It has nothing to do with the GAC. Mattias -- Mattias Sjögren [C# MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com Please reply only to the newsgroup. |
|||||||||||||||||||||||