Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 150581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:18:48+00:00 2026-05-11T09:18:48+00:00

I would like to implement conversion between two library classes by Convert.ChangeType in C#.

  • 0

I would like to implement conversion between two library classes by Convert.ChangeType in C#. I can change neither of the two types. For example converting between Guid and byte[].

Guid g = new Guid(); object o1 = g; byte[] b = (byte[]) Convert.ChangeType(o1, typeof(byte[])); // throws exception 

I am aware that Guid provides a ToByteArray() method, but I would like to have that called when Guid is converted to byte[]. The reason behind this is that the conversion also takes place in library code (AseDataAdapter) which I can not modify. So is it possible to define a conversion rule between two types without modifying the sourcecode of either of the two classes?

I was experimenting with TypeConverter, but doesn’t seem to work either:

Guid g = new Guid(); TypeConverter tc = TypeDescriptor.GetConverter(typeof(Guid)); byte[] b2 = (byte[])tc.ConvertTo(g, typeof(byte[])); // throws exception 

The variable tc gets set to System.ComponentModel.GuidConverter which doesn’t support conversions to byte[]. Can I have two TypeConverters for the same class? Even if I could, wouldn’t I need to prepend an attribute to the source code of the class to assign a TypeConverter?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T09:18:48+00:00Added an answer on May 11, 2026 at 9:18 am

    You can change the registered TypeConverter for something using TypeDescriptor.AddAttributes; this isn’t quite the same as Convert.ChangeType, but it may suffice:

    using System; using System.ComponentModel; static class Program {     static void Main()     {         TypeDescriptor.AddAttributes(typeof(Guid), new TypeConverterAttribute(             typeof(MyGuidConverter)));          Guid guid = Guid.NewGuid();         TypeConverter conv = TypeDescriptor.GetConverter(guid);         byte[] data = (byte[])conv.ConvertTo(guid, typeof(byte[]));         Guid newGuid = (Guid)conv.ConvertFrom(data);     } }  class MyGuidConverter : GuidConverter {     public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)     {         return sourceType == typeof(byte[]) || base.CanConvertFrom(context, sourceType);     }     public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)     {         return destinationType == typeof(byte[]) || base.CanConvertTo(context, destinationType);     }     public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)     {         if (value != null && value is byte[])         {             return new Guid((byte[])value);         }         return base.ConvertFrom(context, culture, value);     }     public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)     {         if (destinationType == typeof(byte[]))         {             return ((Guid)value).ToByteArray();         }         return base.ConvertTo(context, culture, value, destinationType);     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 160k
  • Answers 160k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In the CLI specification, a constructor is mandatory for non-static… May 12, 2026 at 11:38 am
  • Editorial Team
    Editorial Team added an answer Not sure if colorpicker is the right one for you. May 12, 2026 at 11:38 am
  • Editorial Team
    Editorial Team added an answer No, it is not possible. Because ASP.NET textbox finally turns… May 12, 2026 at 11:38 am

Related Questions

I have implemented what I thought was a pretty decent representation of MVC in
I've written a small, in house C# program which batch converts between various file
I'm trying to build a bbcode parser, but I'm having quite some problems figuring
SymPy is a great tool for doing units conversions in Python: >>> from sympy.physics

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.