473,324 Members | 2,268 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Is there a way to convert a string representation of a type name into a Type object?


I want to be able to pass in a function a string say: "TextBox"

Then I need a way to convert that string representation into a Type
object so i can search through some controls and check their type.

Then I can do my check: If ( control is type) { //continue }

I'm doing this inside of a recursive function which goes through the
ControlTree.

Thanks in advance,

-Ralph

P.S. I would pass in a Type object to begin with into my recursive
call like typeof(TextBox) but my recursive function breaks and claims
that it doesn't have access to protected type member. I have no idea
why this happens.

Jun 16 '07 #1
6 1952
"Deckarep" <de******@gmail.comwrote in message
news:11*********************@e9g2000prf.googlegrou ps.com...
P.S. I would pass in a Type object to begin with into my recursive
call like typeof(TextBox) but my recursive function breaks and claims
that it doesn't have access to protected type member. I have no idea
why this happens.
Please show us the code which you are using, and the error message(s)
produced...
--
http://www.markrae.net

Jun 16 '07 #2
The string approach is probably going to be ugly compared with a Type
approach; can you describe what went wrong / what you tried?
Personally I'd rather try and fix this the right way. Getting a Type
from a string is possible, but is easiest if you have the fully-
qualified name. Without it, a more pragmatic approach might be to test
whether eachControl.GetType().Name == theNameYouKnow.

I don't have it to hand, but on a machine that is shut down for the
weekend (miles away) I have a utility function something like (notepad
code):

static void ActionAll<T>(Control control, Action<Taction) where T :
Control {
if(control == null) throw new ArgumentNullException("control");
if(action == null) throw new ArgumentNullException("action");
T t = control as T;
if(t!=null) action(t);
foreach(Control child in control.Controls) {
ActionAll<T>(child, action);
}
}

[I might have optimised it with a queue/stack to avoid recursion, and
avoid repeatedly testing for null]

This allows me to say (as an example):

ActionAll<TextBox>(this, delegate(TextBox tb) {
tb.SomePropertySpecificToTextBox = "blah";
tb.SomeTextBoxSpecificMethod("bloop");
});

You get the idea...

Marc

Jun 16 '07 #3
On Jun 16, 3:20 pm, Marc Gravell <marc.grav...@gmail.comwrote:
The string approach is probably going to be ugly compared with a Type
approach; can you describe what went wrong / what you tried?
Personally I'd rather try and fix this the right way. Getting a Type
from a string is possible, but is easiest if you have the fully-
qualified name. Without it, a more pragmatic approach might be to test
whether eachControl.GetType().Name == theNameYouKnow.

I don't have it to hand, but on a machine that is shut down for the
weekend (miles away) I have a utility function something like (notepad
code):

static void ActionAll<T>(Control control, Action<Taction) where T :
Control {
if(control == null) throw new ArgumentNullException("control");
if(action == null) throw new ArgumentNullException("action");
T t = control as T;
if(t!=null) action(t);
foreach(Control child in control.Controls) {
ActionAll<T>(child, action);
}

}

[I might have optimised it with a queue/stack to avoid recursion, and
avoid repeatedly testing for null]

This allows me to say (as an example):

ActionAll<TextBox>(this, delegate(TextBox tb) {
tb.SomePropertySpecificToTextBox = "blah";
tb.SomeTextBoxSpecificMethod("bloop");

});

You get the idea...

Marc

private void RecurseFindControlTree( ControlCollection cc, Type
typeToFind, List<ControlfoundList )
{
foreach(Control c in cc)
{

if( c is typeToFind)
foundList.Add(c);

RecurseFindControlTree(c.Controls, t, foundList);
}
}
I'm trying to do this which is not working...the compilar says "Can't
access protected member typeToFind" but type is a local member to
this recursive function. I'm not even doing anything sophisticated
just some dirty recursion to get some reporting done for my controls.
I know there's a bunch of better ways to do this but I just want to
get the code working of what I showed above.

Jun 16 '07 #4
"Deckarep" <de******@gmail.comwrote in message
news:11**********************@o11g2000prd.googlegr oups.com...
private void RecurseFindControlTree( ControlCollection cc, Type
typeToFind, List<ControlfoundList )
{
foreach(Control c in cc)
{

if( c is typeToFind)
foundList.Add(c);

RecurseFindControlTree(c.Controls, t, foundList);
}
}
[...] the compilar says "Can't
access protected member typeToFind" but type is a local member to
this recursive function.
"is" does not allow you to use a variable in this way; it expects a type
that is known to the compiler. You can make it work if you pass the Type as
a Generic parameter like this:
private void RecurseFindControlTree<typeToFind>(ControlCollecti on cc,
List<ControlfoundList)
{
foreach (Control c in cc)
{

if (c is typeToFind)
foundList.Add(c);

RecurseFindControlTree<typeToFind>(c.Controls, foundList);
}
}

Jun 17 '07 #5
Deckarep <de******@gmail.comwrote:
I want to be able to pass in a function a string say: "TextBox"

Then I need a way to convert that string representation into a Type
object so i can search through some controls and check their type.
If you pass in the fully-qualified name of the type (including
namespace), you can use Type.GetType. However, be warned that it will
only search mscorlib and the currently executing assembly unless you
*also* provide the assembly details. Look at Type.GetType for more
information. If the types you're interested in are always from the same
assembly, you could use Assembly.GetType.
Then I can do my check: If ( control is type) { //continue }
No, you can't do it like that - you need to use Type.IsAssignableTo (or
a similar method). The right hand operand of "is" is the name of a type
at compile time.
P.S. I would pass in a Type object to begin with into my recursive
call like typeof(TextBox) but my recursive function breaks and claims
that it doesn't have access to protected type member. I have no idea
why this happens.
I'd try to fix that rather than using Type.GetType.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 17 '07 #6
Hi Jon,

I don't think there's a need to post my application.

I can clearly see that trying to check for my type via the 'is' syntax
is what was wrong.

I thought I can just a check for 'is' against a Type object...but that
simply won't do.

Anyways, thanks for all the help to everyone...now I have a couple of
solutions to work from. But I think the best solution is just
changing my code to use generics which has worked like a charm.

And by the way Jon I didn't know I could use Assembly.GetType( string
s ). This will definately help with some situations where I'm still
working with 1.1 code.

Thanks again,

-Ralph
Jun 18 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Fu Bo Xia | last post by:
anyone know a Java (or Java usable) package that converts XML Schema (XSD) documents into a tree form? thanks, fu bo
6
by: Markus Hämmerli | last post by:
I ' ll tra to convert a Cstring to char* without success. I working in a Unicode enabled environment this is working in Unicode CString source = _T("TestString"); TCHAR *szSource =...
4
by: Eric Lilja | last post by:
Hello, I've made a templated class Option (a child of the abstract base class OptionBase) that stores an option name (in the form someoption=) and the value belonging to that option. The value is...
7
by: Philipp H. Mohr | last post by:
Hello, I am trying to xor the byte representation of every char in a string with its predecessor. But I don't know how to convert a char into its byte representation. This is to calculate the...
13
by: Hako | last post by:
I try this command: >>> import string >>> string.atoi('78',16) 120 this is 120 not 4E. Someone can tell me how to convert a decimal number to hex number? Can print A, B, C,DEF. Thank you.
5
by: manmit.walia | last post by:
Hello All, I am stuck on a conversion problem. I am trying to convert my application which is written in VB.NET to C# because the project I am working on currently is being written in C#. I tried...
8
by: davihigh | last post by:
My Friends: I am using std::ofstream (as well as ifstream), I hope that when i wrote in some std::string(...) with locale, ofstream can convert to UTF-8 encoding and save file to disk. So does...
5
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public...
19
by: est | last post by:
From python manual str( ) Return a string containing a nicely printable representation of an object. For strings, this returns the string itself. The difference with repr(object) is that...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.