January 2004 - Posts

Serialization and Inheritence

My problem with my approach in my prototype code was that I inherited from a class that implemented ISerializable. Normal serialization uses reflection to determine the members of an object to serialize (it's dynamic). An object that implements ISerializable usually does so to implement its own serialization -- i.e. not using reflection. Therefore, when you derive from a class that implementes ISerializable, the serialization probably won't see any extra fields/properties you've added to the class (I tested this in my prototype and that is exactly what happened when deriving from Hashtable).

I changed the prototype code to derive from DictionaryBase and all was well in the world (well, at least with my serializing...).

Posted by PSteele | with no comments

Cont'd: Deserialization Problems

Anyone see the big problem with my approach below? I actually thought about this last night but didn't test it until this morning -- and my hunch was right. I'll post more when I get some time at lunch...

Posted by PSteele | with no comments

Deserialization Problems

While prototyping something today, I had an object that I wanted to be able to serialize out to disk. This object was derived from Hashtable, which already implemented the ISerializable interface so I thought I was all set. Serialization worked fine. Then I went to deserialize it and I got the following error:

The constructor to deserialize an object of type ... was not found

I was slightly confused until I looked at the documentation for the Hashtable's constructor. The constructor for deserializing was protected. I needed to make a public constructor and call the base class' constructor:

public MyClass(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
Posted by PSteele | 9 comment(s)

Spirit's problem? Could be buffer overflow.

Reconstructing Spirit's hopeful road to recovery

"Right now, our most likely candidate for the issue has been narrowed down a little bit. It is really an issue with the file system in flash. Essentially, the amount of space required in RAM to manage all of the files we have in flash is apparently more than we initially anticipated.
Posted by PSteele | 1 comment(s)

Versioning in .NET

I was doing some prototype work and wanted to investigate versioning issues. I was changing version numbers of some dependent assemblies and not having any problems with .NET finding the right version. I was puzzled at some of my findings so I checked the docs. My bad:

Note Versioning is done only on assemblies with strong names.

I wasn't using strong named assemblies. That explains a few things... :)

Posted by PSteele | 2 comment(s)

C#'s const vs. readonly

A quick synopsis on the differences between 'const' and 'readonly' in C#:

'const':
  • Can't be static.
  • Value is evaluated at compile time.
  • Initiailized at declaration only.
'readonly':
  • Can be either instance-level or static.
  • Value is evaluated at run time.
  • Can be initialized in declaration or by code in the constructor.
Posted by PSteele | 9 comment(s)

VS.NET Tip

Here's a simple VS.NET Tip:

When you double-click on a form in solution explorer, VS.NET will open up the form designer. You spend some time designing the look of your form and eventually, you're in the code -- full time. But every time you double-click on the form, the form designer comes back up. Wouldn't it nice be to double-click and jump right into code?

Right-click on a form file in solution explorer and select "Open With...":

In the dialog that pops up, select the "CSharp Editor" (or whatever language you use) and click the "Set As Default" button:

Now you're just a double-click away from the code!

Posted by PSteele | 2 comment(s)

Be careful with filtered Exceptions

While researching a good exception handling strategy for the company I work for, I came across an interesting issue with filtered exceptions.

For those who may not know, managed extensions for C++ and VB.NET expose .NET's ability to do filtered exceptions (C# does not allow this). A filtered exception is basically "only catch this exception under certain circumstances". Here's a silly example:

Try
    DoSomething()
Catch ex1 As ArgumentException When someVar = 19
    ' blah, blah, blah
Catch ex2 As Exception when someVar < 5
    ' blah, blah
Finally
    ' more blah
End Try

The expression of the "When" clause can be anything -- including a function call:

Catch e As LowBalanceException When GracePeriodIsOver()

As a quick review of how exceptions are caught and processed, here's a recap from the .NET SDK docs:

When an exception occurs, the system searches for the nearest catch clause that can handle the exception, as determined by the run-time type of the exception. First, the current method is searched for a lexically enclosing try statement, and the associated catch clauses of the try statement are considered in order. If that fails, the method that called the current method is searched for a lexically enclosing try statement that encloses the point of the call to the current method. This search continues until a catch clause is found that can handle the current exception, by naming an exception class that is of the same class, or a base class, of the run-time type of the exception being thrown. A catch clause that doesn't name an exception class can handle any exception.

Of course, we can't forget the finally clause that allows us "clean things up":

Once a matching catch clause is found, the system prepares to transfer control to the first statement of the catch clause. Before execution of the catch clause begins, the system first executes, in order, any finally clauses that were associated with try statements more nested that than the one that caught the exception.

To make things interesting, let's repeat one part of the "try" documentation from above:

...the associated catch clauses of the try statement are considered in order

What this means, is that you can have code execute outside of the try/catch/finally block before the finally block is executed. In the example I showed above, if .NET was looking for a matching catch clause on an exception and found the "When GracePeriodIsOver()" clause, it would have to run that method before determining if the catch block should, in fact, process the exception. The .NET docs covers this under "Securing Exception Handling". Why is it placed under that heading? Because of this:

Try
    IncreasePermissions()
    ' do stuff
Catch ex As Exception When CheckDates()
    ' stuff
Catch ex As Exception When CheckTimes()
    ' more stuff
Finally
    RestorePermissions()
End Try

In the code above, the CheckDates() and CheckTimes() functions will be running under increased permissions! Here's the link to the documentation that prompted me to blog about this. An interesting side-effect of filtered exceptions I never really thought about.

Posted by PSteele | with no comments

Mars curse continues...

Before Spirit's triumphant landing, many news stories talked about the 66% failure rate of Mars land missions. Spirit's mission has been smooth -- until today:

NASA scientists said on Thursday they had lost contact with the robot rover Spirit on Mars and were unsure what had caused the problem.
Posted by PSteele | with no comments

Application Isolation and Assembly.LoadFrom

A thread on the Developmentor ADVANCED-DOTNET list produced a nice link explaining some of the intricacies of Assembly.LoadFrom. Here's a snippet:

However, there are a few intricacies to using LoadFrom that are useful for developers to be aware of. For example, you may run across situations in which calling LoadFrom doesnt load the exact file you expect. Also, understanding the order in which the CLR looks for a LoadFromd assemblys dependencies will help ensure that the CLR loads the correct dependency for your application.
Posted by PSteele | with no comments
More Posts Next page »