-
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.
-
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!
-
-
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?
-
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
-
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..
-
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
-
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..
-
sorry pPerm should be pProps
Sri
-
Yeah.. Friend..I know.. Thanks..
But still error...
-
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
-
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...
Similar Threads
-
Replies: 3
Last Post: 10-02-2005, 11:57 PM
-
By mdengler in forum ASP.NET
Replies: 0
Last Post: 11-26-2002, 03:32 PM
-
By pavel in forum VB Classic
Replies: 0
Last Post: 10-17-2001, 08:06 PM
-
By Chandra in forum VB Classic
Replies: 0
Last Post: 06-22-2000, 12:52 PM
-
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
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|