Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 163537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:35:05+00:00 2026-05-11T11:35:05+00:00

In an assembly loaded into the current AppDomain using Assembly.LoadFrom , I have the

  • 0

In an assembly loaded into the current AppDomain using Assembly.LoadFrom, I have the following code:

[TypeConverter(typeof(EnumConverter<Shapes>))] public enum Shapes {   Triangle,   Square,   Circle } 

The generic EnumConverter<T> is defined in the assembly returned by calling Assembly.GetEntryAssembly(). When I read the TypeConverter attribute at runtime, I get the full name of the type which looks something like:

MyAssembly.EnumConverter`1[[MyDynamicAssembly.Shapes, MyDynamicAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] 

However, when I call Type.GetType() on the string, it returns null. I expected to be able to get an instance of System.Type. Also note that MyDynamicAssembly was not generated by Reflection.Emit (pointed out in the MSDN article here).

This code was generated on Visual Studio 2005 using .NET Framework 2.0.

Has anyone found a workaround for this bug/limitation? Has it been fixed in 3.5?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T11:35:06+00:00Added an answer on May 11, 2026 at 11:35 am

    After following a lead from an MSDN forum posting, I was able to put together a general solution for loading arbitrary generic types from assemblies loaded at runtime. I hope this helps some people (and maybe Microsoft will put something equivalent into .NET 4.0?)

    public static class TypeHelpers {     /// <summary>     /// Gets the System.Type with the specified name, performing a case-sensitive search.     /// </summary>     /// <param name='typeName'>The assembly-qualified name of the type to get. See System.Type.AssemblyQualifiedName.</param>     /// <param name='throwOnError'>Whether or not to throw an exception or return null if the type was not found.</param>     /// <param name='ignoreCase'>Whether or not to perform a case-insensitive search.</param>     /// <returns>The System.Type with the specified name.</returns>     /// <remarks>     /// This method can load types from dynamically loaded assemblies as long as the referenced assembly      /// has already been loaded into the current AppDomain.     /// </remarks>     public static Type GetType(string typeName, bool throwOnError, bool ignoreCase)     {         if(string.IsNullOrEmpty(typeName))             throw new ArgumentNullException('typeName');          // handle the trivial case         Type type;         if((type = Type.GetType(typeName, false, ignoreCase)) != null)             return type;          // otherwise, perform the recursive search         try         {             return GetTypeFromRecursive(typeName, ignoreCase);         }         catch(Exception e)         {             if(throwOnError)                 throw;         }          return null;     }      #region Private Static Helper Methods      private static Type GetTypeFromRecursive(string typeName, bool ignoreCase)     {         int startIndex = typeName.IndexOf('[');         int endIndex = typeName.LastIndexOf(']');          if(startIndex == -1)         {             // try to load the non-generic type (e.g. System.Int32)             return TypeHelpers.GetNonGenericType(typeName, ignoreCase);         }         else         {             // determine the cardinality of the generic type             int cardinalityIndex = typeName.IndexOf('`', 0, startIndex);             string cardinalityString = typeName.Substring(cardinalityIndex + 1, startIndex - cardinalityIndex - 1);             int cardinality = int.Parse(cardinalityString);              // get the FullName of the non-generic type (e.g. System.Collections.Generic.List`1)             string fullName = typeName.Substring(0, startIndex);             if(typeName.Length - endIndex - 1 > 0)                 fullName += typeName.Substring(endIndex + 1, typeName.Length - endIndex - 1);              // parse the child type arguments for this generic type (recursive)             List<Type> list = new List<Type>();             string typeArguments = typeName.Substring(startIndex + 1, endIndex - startIndex - 1);             foreach(string item in EachAssemblyQualifiedName(typeArguments, cardinality))             {                 Type typeArgument = GetTypeFromRecursive(item, ignoreCase);                 list.Add(typeArgument);             }              // construct the generic type definition             return TypeHelpers.GetNonGenericType(fullName, ignoreCase).MakeGenericType(list.ToArray());         }     }      private static IEnumerable<string> EachAssemblyQualifiedName(string s, int count)     {         Debug.Assert(count != 0);         Debug.Assert(string.IsNullOrEmpty(s) == false);         Debug.Assert(s.Length > 2);          // e.g. '[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]'         // e.g. '[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.DateTime, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]'         // e.g. '[System.Collections.Generic.KeyValuePair`2[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.DateTime, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]'          int startIndex = 0;         int bracketCount = 0;          while(count > 0)         {             bracketCount = 0;              for(int i = startIndex; i < s.Length; i++)             {                 switch(s[i])                 {                     case '[':                         bracketCount++;                         continue;                      case ']':                         if(--bracketCount == 0)                         {                             string item = s.Substring(startIndex + 1, i - startIndex - 1);                             yield return item;                             startIndex = i + 2;                         }                         break;                      default:                         continue;                 }             }              if(bracketCount != 0)             {                 const string SR_Malformed = 'The brackets are unbalanced in the string, '{0}'.';                 throw new FormatException(string.Format(SR_Malformed, s));             }              count--;         }     }      private static Type GetNonGenericType(string typeName, bool ignoreCase)     {         // assume the type information is not a dynamically loaded assembly         Type type = Type.GetType(typeName, false, ignoreCase);         if(type != null)             return type;          // otherwise, search the assemblies in the current AppDomain for the type         int assemblyFullNameIndex = typeName.IndexOf(',');         if(assemblyFullNameIndex != -1)         {             string assemblyFullName = typeName.Substring(assemblyFullNameIndex + 2, typeName.Length - assemblyFullNameIndex - 2);             foreach(Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())             {                 if(assembly.GetName().FullName == assemblyFullName)                 {                     string fullName = typeName.Substring(0, assemblyFullNameIndex);                     type = assembly.GetType(fullName, false, ignoreCase);                     if(type != null)                         return type;                 }             }         }          // no luck? blow up         const string SR_TypeNotFound = 'The type, '{0}', was not found.';         throw new ArgumentException(string.Format(SR_TypeNotFound, typeName), 'typeName');     }      #endregion } 

    This code was tested both with the scenario above, and with the following MbUnit test:

    [Test] public void GetType_DictionaryOfStringAndDictionaryOfInt32AndKeyValuePairOfStringAndListOfInt32() {     Dictionary<string, Dictionary<int, KeyValuePair<string, List<int>>>> obj =      new Dictionary<string, Dictionary<int, KeyValuePair<string, List<int>>>>();      string typeName = obj.GetType().FullName;     Type type = TypeHelpers.GetType(typeName, true, false);      Assert.IsTrue(type.Equals(obj.GetType())); } 

    Note: You should comment out the trivial handler when trying to use this test, otherwise Type.GetType() will be called instead of the actual parsing code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 193k
  • Answers 193k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Not you can't. Unless the navigation is triggered by an… May 12, 2026 at 6:29 pm
  • Editorial Team
    Editorial Team added an answer Passwords and senstitive data are best not included the source… May 12, 2026 at 6:29 pm
  • Editorial Team
    Editorial Team added an answer Add #import <QuartzCore/QuartzCore.h> to your file May 12, 2026 at 6:29 pm

Related Questions

Assuming I have only the class name of a generic as a string in
I'm writing a prototype to prove the feasibility of something I'm working on. Basically,
I have been working through a number of very odd issues with some DLL
Is there a way to modify existing .NET assemblies without resorting to 3rd party

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.