views:

55

answers:

2

I need to look at the properties of an object and I cannot instantiate this object in the proper state on my dev machine. I need my client to run some code on her machine, serialize the object in question to disk and then I can analyze the file.

Here is the class I want to serialize.

System.Security.AccessControl.RegistrySecurity

Here is my code:

    Private Sub SerializeRegSecurity(ByVal regKey As RegistryKey)

    Try

        Dim regSecurity As System.Security.AccessControl.RegistrySecurity = regKey.GetAccessControl()

        Dim oXS As XmlSerializer = New XmlSerializer(GetType(System.Security.AccessControl.RegistrySecurity))
        Dim oStmW As StreamWriter

        Dim regDebugFilePath As String = Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "RegDebugFile.xml")
        'Serialize object to XML and write it to XML file
        oStmW = New StreamWriter(regDebugFilePath)
        oXS.Serialize(oStmW, regSecurity)
        oStmW.Close()

    Catch ex As Exception
        Console.WriteLine(ex.ToString)
    End Try

End Sub

And here's what I end up with in my XML file:

<?xml version="1.0" encoding="utf-8"?>

Any ideas on how to accomplish what I am trying to do? How can we serialize a class that is not a custom class of our own?

Thanks for ANY help. Even an alternate method.

A: 

You could try using the BinaryFormatter's Serialize method. This will serialize the object graph to a stream, which you can then persist as required.

Stuart Dunkeld
Thanks, I tried using the BinaryFormatter's Serialize method but I get an exception that states that the System.Security.AccessControl.RegistrySecurity is not marked as serializable.
Doug
+1  A: 

You have several options. Here are the three major ones:

You can write a wrapper class for the class you wish to serialize, then serialize and deserialize the wrapper. On deserialization, the wrapper will need to re-construct the serialized object from its own data, and you'll need to extract the reconstructed object out of the wrapper.

Another option is to implement a Serialization Surrogate. You'll need to manually configure the GetObjectData and SetObjectData methods, which control how data goes into and out of the serialized object.

If the class you wish to serialize is inheritable, and you can work with a subclass instead of the original, you can create a decendant class which implements ISerializable. As with the serialization surrogate, you'll have to deal with GetObjectData yourself.

M.A. Hanin
These options look very promising. But I have NO IDEA how to start implementing any of these. Do you have any links to code samples that may get me started? Thanks! In the mean time I will start Googling to see if I can find some samples to get me going.
Doug
maybe this MSDN article would help: http://msdn.microsoft.com/en-us/magazine/cc188950.aspxAlso check the sample code at the GetObjectData link in my above post
M.A. Hanin