set value by having the value as string represented.

  • Thread starter Thread starter Mr. X.
  • Start date Start date
M

Mr. X.

Hello.
I want to set to any type the value, by sending its string only.
How can I do that ?

I.e
BorderStyle = "None"
(I have "None" value as a string)

Thanks :)
 
I need to not create type editor for each type.
The specific type is unknown, and I need a generic solution for any type.

Thanks :)
 
Am 21.06.2010 13:26, schrieb Armin Zingler:

I guess you will ask...

To create a TypeConverter:
1. Get the type of the property (PropertyInfo.PropertyType)
2. Check the attributes of the type retrived in step 1.
3. If there is a System.ComponentModel.TypeConverterAttribute, get it's 'ConverterTypeName'
property.
4. Pass the name from step 3 to System.Type.GetType
5. Create an instance of the type from step 4 by passing it to Activator.CreateInstance
6. Cast the object from step 5 to System.ComponentModel.TypeConverter. (Before, I'd check
if it's really of that type. Just for safety, because I think the constructor of
TypeConverterAttribute doesn't check the passed type.)
7. Read the documentation about the TypeConverter. :-) The ConvertFromString/ConvertToString
methods seem to do the main job.

I've never done all that. But that's how I'd do it.
 
Am 21.06.2010 14:52, schrieb Mr. X.:
I need to not create type editor for each type.
The specific type is unknown, and I need a generic solution for any type.

Man, I'm really patient. Very patient. Usually. But you guy seem
to be not willing first to learn the things you need for your task.
I don't believe you have already studied the topics in the link I
gave you.

The first line in the first sub topic says:

"You can edit your property in place as a string. This requires a
TypeConverter for your custom type. For more information, see
How to: Implement a Type Converter."

Isn't it what you are looking for?

You ask for a general solution. There is not solution that is more
general than described in the documentation. The TypeConverterAttribute
is the most general solution I know of. I'm not talking about UI type
editors here.
 
Am 21.06.2010 14:52, schrieb Mr. X.:
I need to not create type editor for each type.
The specific type is unknown, and I need a generic solution for any type.

Thanks :)


Concerning Borderstyle: It's an Enum. Look at [Enum].Parse, [Enum].ToString,
[Enum].getvalues, [Enum].Getnames.
 
Armin said:
Am 21.06.2010 14:52, schrieb Mr. X.:

Man, I'm really patient. Very patient. Usually. But you guy seem
to be not willing first to learn the things you need for your task.
I don't believe you have already studied the topics in the link I
gave you.

The first line in the first sub topic says:

"You can edit your property in place as a string. This requires a
TypeConverter for your custom type. For more information, see
How to: Implement a Type Converter."

Isn't it what you are looking for?

You ask for a general solution. There is not solution that is more
general than described in the documentation. The TypeConverterAttribute
is the most general solution I know of. I'm not talking about UI type
editors here.

<VBG>
 
Sorry for being annoying.
And thanks you a lot for being so polite, and patient.

I look at : How to: Implement a Type Converter.
It seems to tell me how to implement a type converter, so I need to do the
conversion by myself finally (at the bottom line),
that's a thing I try to avid (I need to have more general solution).
Maybe I am wrong, but I don't think it's a good solution, that make things
easier.

I get the whole thing from start :
=====================
I am doing a mini-IDE, save it to database (I succeeded doing so, as your
advice),
but retrieving it may be a problem.
(Maybe I should disable some properties, somehow).
I cannot put on database, something which is not string (an object).
The objects are not serializable (and there may be many elements).
So I am using reflection.
When retrieving - I have several objects, which I know only their names and
properties names, and also the properties values.
For an object with 50 properties, and there are several objects, it is
almost impossible to make a translation for every property,
so I need to do something generic.

Maybe I don't understand, but I didn't find any samples around the internet,
that don't have to override the base thing : convertFrom, and convertTo.

Sorry for sending lot of questions.
You indeed succeeded teaching me how to save all the properties to
database/file.
Now I need the opposite : retrieving data from database/file.
Since I am a newbie to that, even doing implementation of interfaces, I need
a detailed sample.

Thanks :)
 
Am 21.06.2010 19:54, schrieb Mr. X.:
Sorry for being annoying.
And thanks you a lot for being so polite, and patient.

I look at : How to: Implement a Type Converter.
It seems to tell me how to implement a type converter, so I need to do the
conversion by myself finally (at the bottom line),
that's a thing I try to avid (I need to have more general solution).
Maybe I am wrong, but I don't think it's a good solution, that make things
easier.


You are not annoying! It would just be nice if you'd look at some
things more thoroughly. Otherwise it's sometimes hard to help. Sure, we
need challenges to develop (ourselves) but I suggest you making smaller
steps before trying to accomplish the big picture. I always play around
with things in test projects before putting it in the actual one.

The information I took from the documentation was how to _use_ a type
converter. I din't write one. I think you've only overlooked my other
reply (from 15:56) with the 7 steps. I've tried it with the Location property
(type Point). The type Point has a TypeConverterAttribute attached. It tells
me that a PointConverter is available for conversion. An instance of it
I get the whole thing from start :
=====================
I am doing a mini-IDE, save it to database (I succeeded doing so, as your
advice),
but retrieving it may be a problem.
(Maybe I should disable some properties, somehow).
I cannot put on database, something which is not string (an object).
The objects are not serializable (and there may be many elements).
So I am using reflection.
When retrieving - I have several objects, which I know only their names and
properties names, and also the properties values.
For an object with 50 properties, and there are several objects, it is
almost impossible to make a translation for every property,
so I need to do something generic.

Maybe you should first determine what _you_ want to store in your database
for each kind of property type.

In the end all types are made of value types, so I'd first determine how to
store them, then recursively store all referenced objects. Just my 2c - I
haven't done anything like that, yet. Be aware that there can be also public
(and private) fields.

Everything depends on the whole task! I don't have a general solution for this.
I think nobody has. There _are_ general concepts like serialization and other
design time support like the BrowsableAttribute, type converters, the
DesignerSerializationVisibility attribute and so on. Apart from this, there are no
general solutions for the task you're trying to accomplish. You must deal with
each "what if..." on your own.
And sometimes there are good reasons why objects are not serializable.

There is nothing more I can say about it.

Maybe I don't understand, but I didn't find any samples around the internet,
that don't have to override the base thing : convertFrom, and convertTo.

No need to override them. Use the existing converters and just call their functions.



Here's an example about type converters. It is not bullet proof code and
it is not meant to be a solution to all your missions: ;-)


Imports System.Reflection
Imports System.ComponentModel
'....

Dim ctl As New Control

Dim t = ctl.GetType

For Each prop In t.GetProperties(BindingFlags.Public Or BindingFlags.Instance)
If prop.CanRead AndAlso prop.CanWrite AndAlso prop.GetIndexParameters.Length = 0 Then
For Each att As System.Attribute In prop.PropertyType.GetCustomAttributes(True)
Dim convAtt = TryCast(att, TypeConverterAttribute)

If convAtt IsNot Nothing Then
Dim convType = Type.GetType(convAtt.ConverterTypeName)

Dim ctorCount = Aggregate count In _
From ctor In convType.GetConstructors _
Where ctor.GetParameters.Count = 0 _
Into Count()

If ctorCount = 1 Then
Dim Converter = TryCast(Activator.CreateInstance(convType), TypeConverter)

If Converter IsNot Nothing Then
Dim value = prop.GetValue(ctl, Nothing)

Debug.Write(prop.Name & ": ")
Debug.WriteLine(Converter.ConvertToString(value))
End If
End If
End If
Next
End If
Next
 
Armin Zingler has brought this to us :
Am 21.06.2010 13:26, schrieb Armin Zingler:

I guess you will ask...

To create a TypeConverter:
1. Get the type of the property (PropertyInfo.PropertyType)
2. Check the attributes of the type retrived in step 1.
3. If there is a System.ComponentModel.TypeConverterAttribute, get it's
'ConverterTypeName' property.
4. Pass the name from step 3 to System.Type.GetType
5. Create an instance of the type from step 4 by passing it to
Activator.CreateInstance 6. Cast the object from step 5 to
System.ComponentModel.TypeConverter. (Before, I'd check if it's really of
that type. Just for safety, because I think the constructor of
TypeConverterAttribute doesn't check the passed type.) 7. Read the
documentation about the TypeConverter. :-) The
ConvertFromString/ConvertToString methods seem to do the main job.

I've never done all that. But that's how I'd do it.

The easiest way to get the type converter is using the
System.ComponentModel.TypeDescriptor object...

Dim converter As TypeConverter = TypeDescriptor.GetConverter(
mypropertyInfo.PropertyType)

If converter IsNot Nothing Then
' do your conversion
Else
' no converter
End If
 
Wow !!!
Great.
Some of the syntax - I didn't know till now (I.e aggregate).
Right now I am testing it,

I see that not of the properties have typeConverter, and I do some check
such as whether property.PropertyType.IsEnum.
I don't know if only properties that their type is ENum don't have
typeConverter.
I have to change my code, in order to check this out - I would let you know
if this totally solved my problem.

Thank you.
I appreciate your efforts. I think I would spend years, if you didn't tell
me that basics.

Thanks :)
 
Great.
One issue - I need to do converter.ConvertToString(aType) when aType is
color, I.e (known only on runtime).

How can I pass a parameter to convertToString, when aType is, I.e., Color ?
If I declare
(When I.e the property that is searched is of type : color)
dim c as color
dim s as string
c = Red
s = converter.ConvertToString (c )
The above works !

but when I do :
dim c
c = color.red
s = converter.ConvertToString( c)
the above doesn't work !
I didn't find a way to pass the correct type to converter.ConvertString
I cannot declare a variable, which is known.
I have tried something like
c = tryCast(property.getValue(myControl, nothing), property.propertyType) '
this is not compiled !!!

Thanks :)
 
Back
Top