Wimdows.NET

Wim's .NET blog

April 2004 - Posts

XmlSerializer - some (enum) field not getting serialized...?

Here's the scenario. I'm serializing a particular object, which contains other objects as properties. I have this particular enum type that won't get included in the serialized XML.

For illustration - I'll simplify the class (which is a property of the main class being serialized); it looks as follows:

public class Contact
{
  public ContactRoleType ContactRole;
  public string ContactID;
  // ....and some more fields...
}


The ContactRoleType is a normal enum. All fields are serialized, except the enum field ContactRole. Strange thing is, when I rename the member field ContactRole, it does get serialized.

I have actually used 
Chris Sells' excellent tool XmlSerializerPreCompiler , and all the types can be serialized without any problem.

All comments and ideas are welcomed!

Thanks!

Posted: Apr 14 2004, 01:34 PM by Wim | with 5 comment(s)
Filed under:
Enum values as bit flags - using FlagsAttribute

The other day, I was using the RegexOptions enum when creating a Regular Expression, and I thought it would be useful to dedicate a blog entry to using enumeration values as flags.

The following is a poor example, but it should be illustrative enough.

Imagine we have a Client class, and one of the Client properties is ClientState. The ClientState enum can be defined as follows:

public enum ClientStates
{
  Ordinary,
  HasDiscount,
  IsSupplier,
  IsBlackListed,
  IsOverdrawn
}

As you can see - these options could be combined in several ways. Rather then creating separate properties in the Client class, we could enable a bitwise combination of enumeration values by using the FlagsAttribute, when defining our enum type. See below.

[Flags]
public enum ClientStates
{
  Ordinary,
  HasDiscount,
  IsSupplier,
  IsBlackListed,
  IsOverdrawn
}


This means we can now use the bitwise OR operator to combine these enum values. For instance:

Client c = new Client();
c.ClientState = (ClientStates.HasDiscount|ClientStates.IsSupplier|ClientStates.IsOverdrawn);

By setting the FlagsAttribute, the values for each enum value now effectively become bitflag patterns that look as follows if you see them in binary form:

00000000  0
00000001  1
00000010  2
00000100  4
00001000  16
00010000  32
00100000  64
01000000  128


As you can see, this bit pattern in our enum values enables us to use the bitwise operator and combine enum values. It is plain to see that all enum values combined, using the bitwise OR, would result in
11111111.

So, how do we check what particular ClientState values have been set? The key is the bitwise AND operator. Let's take our existing Client object 'c'.

[Flags]
public enum ClientStates
{
  Ordinary,       // 0000
  HasDiscount,    // 0001
  IsSupplier,     // 0010
  IsBlackListed,  // 0100
  IsOverdrawn     // 1000
}

So for instance, if we want to check c.ClientState for ClientStates.HasDiscount we can use the following expression:

(c.ClientState & ClientStates.HasDiscount) == ClientStates.HasDiscount

Assuming that c.ClientState contains HasDiscount, IsSupplier and IsOverdrawn, the bit patterns would look like:

c.ClientState:                1011
ClientStates.HasDiscount:     0001
(Logical AND):                0001


So by AND’ing the bit value to check for with the combined bit pattern, we either get the same enum bit pattern value back, or we get a bit pattern which only contains zero’s.

Here’s a little helper method you can use to check for particular ClientStates enum flag values:

public static bool ContainsClientState(ClientStates combined, ClientStates checkagainst)

{

  return ((combined & checkagainst)==checkagainst);
}


That’s it for now. Feel free to leave any comments. Cheers!

Posted: Apr 07 2004, 05:08 PM by Wim | with 41 comment(s)
Filed under:
WebRequest and SSL (The underlying connection was closed. Could not establish trust relationship with remote server.)

internal class AcceptAllCertificatePolicy : ICertificatePolicy
{
  public
AcceptAllCertificatePolicy()
  {
  }

  public bool CheckValidationResult(ServicePoint sPoint,
    
X509Certificate cert, WebRequest wRequest,int certProb)
 
{
   
// Always accept
   
return true;
  
}
}

Yep - that's what you need when you want to make sure that your programmatic SSL WebRequest accepts the SSL challenge. At least when you are encountering the Exception: "The underlying connection was closed. Could not establish trust relationship with remote server."

Before you call call the web request, you then set the static property CertificatePolicy on the ServicePointManager class like so:


ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy();

And that should work like a treat!

Posted: Apr 02 2004, 12:51 PM by Wim | with 25 comment(s)
Filed under:
Random records from table

Hi all,

Great to be part of this blogging community. I will try to submit several posts each week and will make sure they contain snippets of code here and there.

Now, what would you do to select a random set of several records from a SQL Server database table?

A couple of months ago I came across the following simple, but yet very effective solution to do just that. For instance to get 10 random quotes from a Quotes table:

SELECT TOP 10 * FROM Quotes ORDER BY NEWID()

Personally I think it's pure gold.

Posted: Apr 01 2004, 10:21 PM by Wim | with 2 comment(s)
Filed under:
More Posts