none
Type.GetType problem RRS feed

  • Question

  • Hi,

    I have stored the type of a class in string by using reportPropertieType.CollectionInnerType.FullName and it looks like this : MyApp.Business.Entity.EIOrdineradUtredning+OrdUtredningItem

    I am then trying to run this : Type.GetType(MyApp.Business.Entity.EIOrdineradUtredning+OrdUtredningItem); but I get null?

    How should I get a Type from this string?

    //SnowJim

    Monday, June 7, 2010 1:30 PM

Answers

  • Type.GetType requires the AssemblyQualifiedName, not the FullName of the type. FullName only works with types from mscorlib.

    See the documentation for Type.GetType: http://msdn.microsoft.com/en-us/library/w3f99sx1.aspx

    • Marked as answer by SnowJim Tuesday, June 8, 2010 8:56 AM
    Monday, June 7, 2010 3:29 PM
    Moderator
  • "As you maby understand, the object that contains the AssemblyQualifiedName is Serilized and then converted to string. Then I am converting back to binary array and the deserilizing the object."

    The result of serialization is binary data. Binary data cannot be converted to a string the way you're trying to do because not all binary data represents a valid string. The only safe way to do something like this is to use Convert.ToBase64String.

    • Marked as answer by SnowJim Tuesday, June 8, 2010 8:55 AM
    Tuesday, June 8, 2010 8:24 AM
    Moderator

All replies

  • Type.GetType requires the AssemblyQualifiedName, not the FullName of the type. FullName only works with types from mscorlib.

    See the documentation for Type.GetType: http://msdn.microsoft.com/en-us/library/w3f99sx1.aspx

    • Marked as answer by SnowJim Tuesday, June 8, 2010 8:56 AM
    Monday, June 7, 2010 3:29 PM
    Moderator
  • Thanks!

    I have now tried this but a new problem appeared before I could check if it works.

    The object that I store the AssemblyQualifiedName in are serilized and deserilized like this :

     

    public static byte[] SerializeObject(Object objectToSerialize)
        {
          MemoryStream memoryStream = new MemoryStream();
          BinaryFormatter binaryFormatter = new BinaryFormatter();
          
          try
          {
           
            //Here's the core functionality! One Line! 
    
            //To be thread-safe we lock the object
            
            lock (locker)
            {
              binaryFormatter.Serialize(memoryStream, objectToSerialize);
              memoryStream.Close();
            }
            return memoryStream.ToArray();
          }
          catch (SerializationException se)
          {
            throw se;
          }
          catch (Exception ex)
          {
            
            throw ex;
          }
            
          finally
          {
            if (memoryStream != null)
              memoryStream.Close();
          }
        }

    and

    public static object DeserializeObject(byte[] byteArrayToDeserialize)
        {
          MemoryStream memoryStream = null;
          BinaryFormatter binaryFormatter = new BinaryFormatter();
          try
          {
            if (byteArrayToDeserialize != null && byteArrayToDeserialize.Length > 0)
            {
              lock (locker)
              {
                memoryStream = new MemoryStream(byteArrayToDeserialize);
                memoryStream.Position = 0;
    
                return binaryFormatter.Deserialize(memoryStream);
              }
            }
            return null;
          }
          catch (SerializationException se)
          {
            throw se;
          }
          finally
          {
            if(memoryStream != null)
              memoryStream.Close();
          }
        }

     

    To convert between binary array and string I am using these :

    public static byte[] StringToByteArray(string input)
        {
          System.Text.ASCIIEncoding encoder = new System.Text.ASCIIEncoding();
          return encoder.GetBytes(input);
        }
        public static string ByteArrayToString(byte[] input)
        {
          System.Text.ASCIIEncoding encoder = new System.Text.ASCIIEncoding();
          return encoder.GetString(input);
        }

     

    As you maby understand, the object that contains the AssemblyQualifiedName is Serilized and then converted to string. Then I am converting back to binary array and the deserilizing the object.

    As soon as the AssemblyQualifiedName string is set in my object the following exception is thrown when deserializing :

    Binary stream114 do not contain a  valid BinaryHeader. Possible  Möjliga caouse : invalid stream or the object version have changed between serialization and deserialization.

    When replcing the AssemblyQualifiedName with a empty string everything works fine?


     

    Tuesday, June 8, 2010 7:39 AM
  • "As you maby understand, the object that contains the AssemblyQualifiedName is Serilized and then converted to string. Then I am converting back to binary array and the deserilizing the object."

    The result of serialization is binary data. Binary data cannot be converted to a string the way you're trying to do because not all binary data represents a valid string. The only safe way to do something like this is to use Convert.ToBase64String.

    • Marked as answer by SnowJim Tuesday, June 8, 2010 8:55 AM
    Tuesday, June 8, 2010 8:24 AM
    Moderator
  • Thanks! That solved the problem!
    Tuesday, June 8, 2010 8:55 AM