How to extract full type name from assembly qualified name

  • Thread starter Thread starter John Brown
  • Start date Start date
J

John Brown

Hi there,

Does anyone know how to extract the full type name ("namespace.type") from
an assembly qualified name. Thanks.
 
Hi there,

Does anyone know how to extract the full type name ("namespace.type") from
an assembly qualified name. Thanks.

I think System.Reflection namespace will help you with that problem.
 
I think System.Reflection namespace will help you with that problem.

That's what I thought but I can't find any relevant class. I thought
"AssemblyName" might help but it throws the following no matter what I pass
it:

"The given assembly name or codebase was invalid. (Exception from HRESULT:
0x80131047)"
 
Thanks for the feedback.
Can you show the code you are using to load the assembly?

I'm not explicitly loading the assembly. I'm simply passing the string to
the "AssemblyName" constructor though I've since discovered it's probably
not the correct class. That is, the exception results when I pass it this
for instance:

"System.String, mscorlib, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"

The constructor takes a "display name" argument according to the docs which
is the above name *minus* the "System.String, " at the start (based on what
I've read about the format of a fully-qualified asssembly name).
"System.String" is what I'm trying to extract however but I don't know if
it's safe to simply parse the string myself. Can I safely assume it will
always precede the first comma that is and will it matter if I run this on a
thread with a different culture (i.e., is the comma culture-sensitive in
thsi context - I wouldn't think so). Preferably I want to use an official
function if one is available. Thanks.
 
Can you show the code you are using to load the assembly?

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)




That's what I thought but I can't find any relevant class. I thought
"AssemblyName" might help but it throws the following no matter what I
pass it:
"The given assembly name or codebase was invalid. (Exception from HRESULT:
0x80131047)"- Hide quoted text -

- Show quoted text -

Are you using GetType() method? THis method expects a fully quliafied
name i guess.

Try using "Assembly.FullName"
 
Are you using GetType() method? THis method expects a fully quliafied
name i guess.

Try using "Assembly.FullName"

I'm not working with the assembly. I'm only working with the
assembly-qualified strings themselves. The actual assembly might not even be
present on the host machine (e.g., 3rd-party assemblies).
 
John,
You absolutely can parse the fully qualified type name yourself. If
you are sure that it is a valid type name, then you can use the following
BNF notation to generate parse rules to get what you are after:

http://msdn2.microsoft.com/en-us/library/yfsftwz6.aspx

Thanks for the link. I was hoping a canned routine would already be natively
available but I'll just have to roll it myself assuming I can't find a
reliable implementation on the web. Anyway, thanks again.
 
Hi,

John Brown said:
Hi there,

Does anyone know how to extract the full type name ("namespace.type") from
an assembly qualified name. Thanks.

what about typeof( YourType) ?
 
Does anyone know how to extract the full type name ("namespace.type")
what about typeof( YourType) ?

Thanks but I don't have the actual type, only the assembly qualified name as
a string. I did find this blurb under "Type.AssemblyQualifiedName" however:

"The assembly-qualified name of a type consists of the type name, including
its namespace, followed by a comma, followed by the display name of the
assembly."

I'm leery about trusting this however since it may be just a passing
comment. I'm now looking through the BNF link that Nicholas provided to
confirm if it's really safe to simply scan for the first comma.
 
John Brown said:
Hi there,

Does anyone know how to extract the full type name ("namespace.type") from
an assembly qualified name. Thanks.

The following works in v1.1 (And should work in all later versions and
possibly even earlier one too):

string qualType =
@"System.String, mscorlib, Version=2.0.0.0, " +
@"Culture=neutral, PublicKeyToken=b77a5c561934e089";

Type t = Type.GetType(qualType, false);

if (t == null) {
Console.WriteLine("Invalid qualified type string.");
return;
}

Console.WriteLine(t.FullName);


HTH :)

Mythran
 
Back
Top