DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 12 of 12

Thread: How to change string to type?

  1. #1
    Join Date
    Dec 2004
    Posts
    717

    How to change string to type?

    Is there any way to convert string to type?
    I put the typename to a string. I like to convert this string to a particular type according that string..
    something like ...
    Code:
      string str = "Int32";
      Type t = ConvertStringToType(str);
      // t should be "Int32"..
    I hope you got my point.. :-)
    Any idea would be greatly appreciated.
    Thanks in advance.
    Best Regards,
    Michael Sync
    http://michaelsync.net

    The more you share,The more you get

  2. #2
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    Type t = System.Type.GetType("System.Int32");
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  3. #3
    Join Date
    Dec 2004
    Posts
    717
    Thank you.. Phil..
    Best Regards,
    Michael Sync
    http://michaelsync.net

    The more you share,The more you get

  4. #4
    Join Date
    Dec 2004
    Posts
    717
    But I'm not able to activate the object if this object is EventArgs..
    Code:
      string str = "Sync.myGrid.ErrorEventArgs";
      Type t = System.Type.GetType(str);
      System.Object o = asm.CreateInstance(t.FullName);
      System.Object o1 = Activator.CreateInstance(Type.GetType(t.Namespace + "." + t.Name));
    How to create the instance of the object that has no constructor?
    Best Regards,
    Michael Sync
    http://michaelsync.net

    The more you share,The more you get

  5. #5
    Join Date
    May 2005
    Location
    UK
    Posts
    278
    Type type = Type.GetType(fully qualified class name string);
    class object = (Class) Activator.CreateInstance(type, null);
    this should do the trick , try it and let me know
    Sri

  6. #6
    Join Date
    Dec 2004
    Posts
    717
    Thanks. Srinivas.
    I like to load a assembly without adding references to the project. eg: http://forums.devx.com/showthread.php?t=148225
    Otherwise, declaraing class will not be known from IDE.Act, we can normally activate the object if this object has contructors. let me ask the previous again "How to create the instance of the object that has no constructor?"
    Another things is that one error come out when the method is called even this object has contructors. Suppose: I activated one object that has constructors. It's fine. Then There is one property called BorderWidth of the object that i activated. I like to get the value of this property "BorderWidth".. I also tried to use Type.Invoke() function but nothing happen..... My another question is how to get the value of property of object after activating the object....
    Thanks in advances..
    Best Regards,
    Michael Sync
    http://michaelsync.net

    The more you share,The more you get

  7. #7
    Join Date
    May 2005
    Location
    UK
    Posts
    278
    Sync I kind of understand your question not, but in C# when you declare a class and an object which represents that class has a default constructor whether we write the code for an empty constructor or not, this is how the compiler will work, if we dont write a constructor the compiler will create a () constructor by itself, thats because
    properties are not created (unless static) without an instance isnt it?
    Refer here for the following
    http://www.c-sharpcorner.com/Code/20...rsInCSharp.asp

    As for getting proprties on the object
    as below
    pToken is the object..
    Type t = pToken.GetType();
    PropertyInfo[] pProps = t.GetProperties();
    for (int indexProperty = 0; indexProperty <pProps .Length; indexProperty ++)
    {
    // Sets the property
    pProps [indexProperty].SetValue(pToken, <this value you set should be the same as the type of the property>,null);
    // Gets the property
    object obj - pPerm[indexProperty].GetValue(pToken, null)
    }

    Hope this helps
    Sri

  8. #8
    Join Date
    Dec 2004
    Posts
    717
    Thank you. srinivas.
    If you have time, you test the following ways that i have problems.
    1) You can get any object inherits from EventArgs. then, activate this object.. You will see one error..
    2) object obj - pPerm[indexProperty].GetValue(pToken, null).. I got one error..
    Best Regards,
    Michael Sync
    http://michaelsync.net

    The more you share,The more you get

  9. #9
    Join Date
    May 2005
    Location
    UK
    Posts
    278
    sorry pPerm should be pProps
    Sri

  10. #10
    Join Date
    Dec 2004
    Posts
    717
    Yeah.. Friend..I know.. Thanks..
    But still error...
    Best Regards,
    Michael Sync
    http://michaelsync.net

    The more you share,The more you get

  11. #11
    Join Date
    May 2005
    Location
    UK
    Posts
    278
    I am not sure Sync because I gave that piece of code from an existing server control i wrote and i use this there and it works , Can you please give me the error in detail?
    Sri

  12. #12
    Join Date
    Dec 2004
    Posts
    717
    sure.. friend..
    I have another problem.. I'm not able to get the type of System.Drawing.Font.
    Code:
     Type t = Type.GetType("System.Drawing.Font");
    Please dont' add your control as references to the project.. If you already added this control as references, please delete it..

    I used this coding to activate the object...
    Code:
    using System.Reflection;
    
    public ArrayList LoadAssembly(string filename)
    {
    
     System.Reflection.Assembly asm = System.Reflection.Assembly.LoadFrom(filename);				 
     System.Reflection.AssemblyName asmName = asm.GetName();			
     
     string _strNamespace = asmName.Name;
     
     System.Reflection.BindingFlags binding = System.Reflection.BindingFlags.Public | 
    				System.Reflection.BindingFlags.Instance | 
    				System.Reflection.BindingFlags.Static;
    
      foreach (Type t in asm.GetTypes())
     {	
    
          bool isEventHandler = t.BaseType == typeof(MulticastDelegate);
          if (t.IsInterface 
    		|| !t.IsPublic
    		|| !IsBrowsable(t)
    		|| t.IsSpecialName
    		|| t.IsEnum
    		|| isEventHandler)
    		continue;
    
           object o = null;
    
           //************  ERROR if this control is EventAgrs
          o = asm.CreateInstance(t.FullName);
        
     }
    }
    The error is "Constructor on type MyNamespace.CustomEventArgs not found."

    Another one ::
    Code:
    using System.Reflection;
    
    public ArrayList LoadAssembly(string filename)
    {
    
     System.Reflection.Assembly asm = System.Reflection.Assembly.LoadFrom(filename);				 
     System.Reflection.AssemblyName asmName = asm.GetName();			
     
     string _strNamespace = asmName.Name;
     
     System.Reflection.BindingFlags binding = System.Reflection.BindingFlags.Public | 
    				System.Reflection.BindingFlags.Instance | 
    				System.Reflection.BindingFlags.Static;
    
      foreach (Type t in asm.GetTypes())
     {	
    
          bool isEventHandler = t.BaseType == typeof(MulticastDelegate);
          if (t.IsInterface 
    		|| !t.IsPublic
    		|| !IsBrowsable(t)
    		|| t.IsSpecialName
    		|| t.IsEnum
    		|| isEventHandler)
    		continue;
    
           object o = null;
    
    
          try
          {
                ConstructorInfo[] ci = t.GetConstructors();
                if(ci.Length > 0)
                {
    
                     //************  Your Coding
    	   //Late binding..
    	   System.Object pToken = asm.CreateInstance(t.FullName);
    	   Type ttt = pToken.GetType();  
                     foreach(System.Reflection.PropertyInfo pProps  in ttt.GetProperties(binding))
    	   {
    		// Gets the property 
    		object obj = pProps.GetValue(pToken, null);
    	              Console.WriteLine(o.ToString());
                     }
    	}					
           }
           catch(Exception ex)
           {
    	Console.WriteLine(ex.Message);
           }    
     }
    }
    Sometimes. it's working fine.. sometimes. i found this err "Object reference not set to an instance of an object."

    Thanks.. Last error is not problem... I really wanna get the value of EventArgs.. Otherwise, i dont wanna check the count of constructos...
    Thanks againn...
    Best Regards,
    Michael Sync
    http://michaelsync.net

    The more you share,The more you get

Similar Threads

  1. Replies: 3
    Last Post: 10-02-2005, 11:57 PM
  2. Input string was not in a correct format
    By mdengler in forum ASP.NET
    Replies: 0
    Last Post: 11-26-2002, 03:32 PM
  3. Change NT service config (disabled -> enabled)
    By pavel in forum VB Classic
    Replies: 0
    Last Post: 10-17-2001, 08:06 PM
  4. Deadlock error.. how to remove
    By Chandra in forum VB Classic
    Replies: 0
    Last Post: 06-22-2000, 12:52 PM
  5. Trying to print a PDF File from VB
    By Kunal Sharma in forum VB Classic
    Replies: 2
    Last Post: 04-25-2000, 03:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center