i've got a bunch of code doing something like
if control == typeof( GridCurrencyTextBox ))
{
((GridCurrencyTextBox)(_control)).Text = ....
}
if control == typeof( TextBox ))
{
((TextBox )(_control)).Text = ....
}
and so o开发者_如何学Cn. i know i can estimate the type via control.GetType(), but the .Text property can only be set knowing the type at compiletime.
i'd like something like:
Type t = _control.getType();
(t(_control)).Text = .....
any suggestions? thanks!
It's hard to tell from your question what you're actually asking, but have you tried:
ITextControl textControl = _control as ITextControl;
if(textControl != null)
{
textControl.Text = //...
}
Casting with as
will give you the default value (null
in this case) if the object you're trying to cast isn't the right type and, as far as I'm aware, most (all?) text controls in .NET derive from ITextControl
(which just defines a Text
property).
This assumes you're using ASP.NET. As mentioned by someone else, in Windows Forms Control
has a Text
property, so you would only need to make sure you casted to Control
, and if it's already a control, the check is unnecessary (except to make sure the value's not null).
If you're using any other type of Control and it doesn't derive from a common type that gives you a Text
property, you might have to resort to using reflection, but this is far from ideal as it will be slow:
PropertyInfo text = _control.GetType().GetProperty("Text", BindingFlags.Public | BindingFlags.Instance, null, typeof(string), Type.EmptyTypes, null);
if(text != null)
{
text.SetValue(obj, /* some value */, null);
}
If you are using .NET 3.0 you can take advantage of the extension methods to rewrite your code too look a little bit better. Each of your comparison consist of few little steps:
- Check for a specific type
- If the object is of the specific type, cast it
- Perform some action on it
Step 1 and 2 can be performed with the as
-operator (your target must be a object then, as the as
operator can only cast reference types). Step 3 can be done via a delegate. Thus a helper function could look like this:
public static class Helper { public static void Do<T>(object obj, Action<T> action) where T : class { T castedObject = obj as T; if (castedObject != null && action != null) action(castedObject); } }
Then you could call:
Helper.Do<TextBox>(control, delegate(TextBox obj) { obj.Text = "your text goes here"; });
... for each type you have to handle. This solution uses a helper class and a anonymous delegate.
This is not the best solution. With extension methods and lambda expression you could have something like:
public static class ObjectExtensions { public static void Do<T>(this T obj, Action<T> action) { if (obj != null && action != null) action(obj); } public static T Cast<T>(this object obj) where T : class { return obj as T; } }
... and use:
control.Cast<TextBox>().Do(t => t.Text = "your text goes here");
for each type you have to handle. This line is even more readable than the previous solution. But in this case you have to know how extension methods and lambdas work.
If you have property and you don't know the type you can always do this:
var type = _control.GetType();
var propertyInfo = type.GetProperty("Text");
if(propertyInfo!=null) propertyInfo.SetValue(_control,"Some Value");
精彩评论