473,385 Members | 1,942 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,385 software developers and data experts.

Problem with type converter

I have written a component with a property IPAdrress of type
System.Net.IPAddress. To ease the configuration of the component at design
time, I have written a type converter for the type System.Net.IPAddress. The
type System.Net.IPAddress, as you know, is provided by Microsoft and I have
no choice but to apply the type converter on the IPAddress property of the
component itself (instead of the type System.Net.IPAddress). All normal
features of a type converter are working perfectly but I just couldn't get
VS.NET 2003 to produce constructor-based property initialization code for the
IPAddress property of the component as follow

this.myComponent.IPAddress = new System.Net.IPAddress(0);

instead of

this.myComponent.IPAddress.Address = 0;
or
this.myComponent.IPAddress =
((System.Net.IPAddress)(resources.GetObject("myCom ponent.IPAddress")));

This wouldn't have happened if I were able to apply the type converter on
the type System.Net.IPAddress itself. How do I get around this? Thanks.

Code of the component (extract) and the type converter follow:

// MyComponent.cs
public class MyComponent : System.ComponentModel.Component
{
// ...
[TypeConverter(typeof(IPAddressConverter))]
public IPAddress IPAddress
{
get
{
return ipAddr;
}
set
{
if(value == null)
throw new ArgumentNullException("IPAddress");
ipAddr = value;
}
}
// ...
}

// IPAddressConverter.cs
using System;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Net;
using System.Net.Sockets;
using System.Reflection;

namespace Util.Net
{
public class IPAddressConverter : System.ComponentModel.TypeConverter
{
private const string ANY = "Any";
private const string LOOPBACK = "Loopback";
private const string ANYIPADDRESS = "0.0.0.0";
private const string LOOPBACKIPADDRESS = "127.0.0.1";

public IPAddressConverter()
{
}

public override bool
CanConvertFrom(System.ComponentModel.ITypeDescript orContext context,
System.Type sourceType)
{
if(sourceType == typeof(string))
return true;
return base.CanConvertFrom(context, sourceType);
}

public override object
ConvertFrom(System.ComponentModel.ITypeDescriptorC ontext context,
System.Globalization.CultureInfo culture, object value)
{
if(value is string)
{
if((string)value == ANY)
value = ANYIPADDRESS;
else if((string)value == LOOPBACK)
value = LOOPBACKIPADDRESS;
return IPAddress.Parse((string)value);
}
return base.ConvertFrom(context, culture, value);
}

public override bool
CanConvertTo(System.ComponentModel.ITypeDescriptor Context context,
System.Type destinationType)
{
if(destinationType == typeof(InstanceDescriptor))
return true;
return base.CanConvertTo(context, destinationType);
}

public override object
ConvertTo(System.ComponentModel.ITypeDescriptorCon text context,
System.Globalization.CultureInfo culture, object value, System.Type
destinationType)
{
if(destinationType == typeof(string) && value is IPAddress)
{
if(value.Equals(IPAddress.Any))
return ANY;
if(value.Equals(IPAddress.Loopback))
return LOOPBACK;
return value.ToString();
}
if(destinationType == typeof(InstanceDescriptor) && value is IPAddress &&
((IPAddress)value).AddressFamily == AddressFamily.InterNetwork)
{
ConstructorInfo constructorInfo = typeof(IPAddress).GetConstructor(
new Type[]{typeof(long)});
if(constructorInfo != null)
{
// return new InstanceDescriptor(constructorInfo, new long[]{
// ((IPAddress)value).Address});
byte[] addressBytes = ((IPAddress)value).GetAddressBytes();
long address = addressBytes[0] | (addressBytes[1] << 8) |
(addressBytes[2] << 16) | (addressBytes[3] << 24);
return new InstanceDescriptor(constructorInfo, new
long[]{address});
}
}
return base.ConvertTo(context, culture, value, destinationType);
}

public override bool
GetStandardValuesSupported(System.ComponentModel.I TypeDescriptorContext
context)
{
return true;
}

public override System.ComponentModel.TypeConverter.StandardValues Collection
GetStandardValues(System.ComponentModel.ITypeDescr iptorContext context)
{
// IPAddress[] specialIPAddresses = {
// new IPAddress(IPAddress.Any.Address),
// new IPAddress(IPAddress.Loopback.Address)};
IPAddress[] specialIPAddresses = {
new IPAddress(0),
new IPAddress(0x0100007F)};
return new StandardValuesCollection(specialIPAddresses);
}
}
}

Jul 21 '05 #1
5 2896
Hi,

First of all, I would like to confirm my understanding of your issue. From
your description, I'm not quite sure what you need to do. Do you mean that
a constructor that can convert as the type converter does, to initialize
the IPAddress? If there is any misunderstanding, please feel free to let me
know.

If so, I think we cannot do with the IPAddress class, since it doesn't have
a constructor that does so. I think you can try to overload the constructor
of IPAddressConverter to achieve this.

Please correct me if my understanding is wrong. Thanks!

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Jul 21 '05 #2
Hi Kevin,

I want my component to be able to generate a constructor-based
initialization for its IPAddress property. For example, if I set the
IPAddress property to be "127.0.0.1", it should generate in the
InitializeComponent() method of its container the following code

this.myComponent.IPAddress = new System.Net.IPAddress(16777343);
// 16777343 is the equivalent of 127.0.0.1

rather than

this.myComponent.IPAddress =
((System.Net.IPAddress)(resources.GetObject("myCom ponent.IPAddress")));

The System.Net.IPAddress type does have a constructor that takes a long
address as parameter. I've written the IPAddressConverter type to accomplish
this. The code is found in the overriden ConvertTo method below but
unfortunately it does not work as expected. Kindly advise. Thanks.

public override object
ConvertTo(System.ComponentModel.ITypeDescriptorCon text context,
System.Globalization.CultureInfo culture, object value, System.Type
destinationType)
{
if(destinationType == typeof(string) && value is IPAddress)
{
if(value.Equals(IPAddress.Any))
return ANY;
if(value.Equals(IPAddress.Loopback))
return LOOPBACK;
return value.ToString();
}
if(destinationType == typeof(InstanceDescriptor) && value is IPAddress &&
((IPAddress)value).AddressFamily == AddressFamily.InterNetwork)
{
ConstructorInfo constructorInfo = typeof(IPAddress).GetConstructor(
new Type[]{typeof(long)});
if(constructorInfo != null)
{
// return new InstanceDescriptor(constructorInfo, new long[]{
// ((IPAddress)value).Address});
byte[] addressBytes = ((IPAddress)value).GetAddressBytes();
long address = addressBytes[0] | (addressBytes[1] << 8) |
(addressBytes[2] << 16) | (addressBytes[3] << 24);
return new InstanceDescriptor(constructorInfo, new
long[]{address});
}
}
return base.ConvertTo(context, culture, value, destinationType);
}

"Kevin Yu [MSFT]" wrote:
Hi,

First of all, I would like to confirm my understanding of your issue. From
your description, I'm not quite sure what you need to do. Do you mean that
a constructor that can convert as the type converter does, to initialize
the IPAddress? If there is any misunderstanding, please feel free to let me
know.

If so, I think we cannot do with the IPAddress class, since it doesn't have
a constructor that does so. I think you can try to overload the constructor
of IPAddressConverter to achieve this.

Please correct me if my understanding is wrong. Thanks!

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Jul 21 '05 #3
Hi,

For this issue, we have reproduce out the problem, it seems that IPAddress
always use binary serialization for code generator. We will spend some more
time on it. We will update you ASAP. Thanks for your understanding.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Jul 21 '05 #4
Hi,

My investigation shows that if I were able to apply the TypeConverter
attribute on the type itself (in my case the System.Net.IPAddress type)
rather than on the property of this type of the component (in my case the
MyComponent.IPAddress property), VS .NET would produce the correct
initialization code. This is hypothetical of course since the
System.Net.IPAddress type is provided by Microsoft. Thanks.

""Jeffrey Tan[MSFT]"" wrote:
Hi,

For this issue, we have reproduce out the problem, it seems that IPAddress
always use binary serialization for code generator. We will spend some more
time on it. We will update you ASAP. Thanks for your understanding.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Jul 21 '05 #5
Hi,

I debugged the sample and can confirm that serialization indeed doesn't
seem to pick up type converters attached to properties. In this case, you
have a type converter attached to a property of type IPAddress and not the
IPAddress class itself, so it is by design that serialization doesn't find
it.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Jul 21 '05 #6

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

Similar topics

7
by: jorfei | last post by:
I have written a component with a property IPAdrress of type System.Net.IPAddress. To ease the configuration of the component at design time, I have written a type converter for the type...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
2
by: ThunderMusic | last post by:
Hi, I have a value that contains flags that I must get using a bitmask. I tryied with the && operator, but the compiler outputs this error : Operator '&&' cannot be applied to operands of type...
8
by: bonk | last post by:
When I have an instance of an object wich is of type System.Type, how can I find out if it directly or indirecly derives from another type? myTypeInstance == typeof(AnotherType) only seems to...
8
by: coosa | last post by:
Dear all, I have the following code: #include <iostream> #include <string> #include <sstream> using namespace std;
7
by: Mike Howard | last post by:
I'm trying to convert the following (simplified) VB.Net code to C#, that makes use of some externally developed COM code written in VB6. VB.Net Code Sub Main() Dim oAPP As Object Dim oApp2...
3
by: AAJ | last post by:
Hi has anyone come across a function to check if a particular string can be safely converted to a datatype i.e. i would like to check things like TypeCheck("1/1/2006",datetime) -returns...
1
by: Robert Dufour | last post by:
I am trying to sort the membership list of the membership provider for ASP.NET the code has retrieved the list and now we need to sort it by a parameter if it was passed. So in C# the code is....
6
by: Tony Johansson | last post by:
Hello! We have a C#.ASP.NET application that runs on a IIS 6. The application contains actually several asp pages but there is no GUI. The application receive an xml file that is processed....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.