Posts for #C#

I recently saw this question regarding the use of methods that get passed delegates – specifically, the .NET 2 collection classes make use of this, like for example the List<T> class, which takes a Predicate<T> as a parameter to its Find method. Now the question is how to make use of this method? How does the delegate know what to do when it’s called?

As I see it, there are two (good) approaches to this: using an anonymous method and using a separate class. I’m going to illustrate both approaches below, with a sample that makes a few assumptions:

I guess you know the way you can define aliases with the using statement:

using SWF = System.Windows.Forms;

This blog article explains that, and why, a late change to the implementation of nullable types is being made in the runtime, making nullables an intrinsic type. This solves the problems that many had reported when nullables were used in situations where boxing occurred, and where null comparisons were made involving generic types. Read the original article for the details, this is good news!

I just came upon this question in a newsgroup and had a quick look at the new classes that .NET 2 offers for access to the Windows file system ACLs and general system security information. In short, to get to the name of a user or group that’s the owner of a directory in the file system, you can use the following code:

DirectoryInfo directoryInfo = new DirectoryInfo(@"c:\Windows");
DirectorySecurity directorySecurity =
  directoryInfo.GetAccessControl(AccessControlSections.Owner);
IdentityReference identityReference =
  directorySecurity.GetOwner(typeof(NTAccount));
// now access identityReference.Value for the readable name

I’m using Developer Express XtraBars in my .NET applications. With one of the latest releases, XtraBars also support the new skinning technology, which the website depicts quite inadequately but which takes the experience of an application based completely on DX controls to a new level visually. Now who would have thought that a package such as this would have grave deficiencies in other areas? Well, custom drawing is one such area.

Recently I wanted to create a simple bar item that would be able to specify its own background and foreground colours, independent of the configured paint style/skinning. I played with deriving my own BarItem, BarItemLink, BarLinkViewInfo, PrimitivesPainter, PaintStyle and several other classes for the larger part of a day, until I finally decided that the current structure of the whole XtraBars painting is such that this simple modification is enormously complicated, IF I want to stay within the extensibility structure that’s available out of the box.

A while ago, I read a tiny note about this in somebody’s blog and I want to start off by apologizing to that somebody because for the life of me I can’t figure out who it might have been – so no reference here. Sorry about that. Anyway, I followed up on the original information and I thought it couldn’t hurt to pull peoples’ attention to this a bit more. So, the ?? operator: It’s an extension in C# 2.0 (find the specs here, this operator is explained on page 14, then end of chapter 19.5) and they call it the null coalescing operator. Here’s a conditional expression, in three different forms, but with the same meaning:

MyClass anObject;
...
// Variant 1: Using a full if/else clause
MyClass anotherObject;
if (anObject != null)
  anotherObject = anObject;
else
  anotherObject = new MyClass();

// Variant 2: Using the ?/: conditional operator
MyClass anotherObject = anObject != null ? anObject : new MyClass();

// Variant 3: Using the ?? operator
MyClass anotherObject = anObject ?? new MyClass();

I just stumbled upon a funny problem related to the new .NET 2.0 attribute InternalsVisibleTo in conjunction with a strong-named assembly. The situation is this: I have that assembly that exposes its internals to another assembly (for unit testing) using InternalsVisibleTo.

Now I decided to sign that assembly with a strong name. Suddenly, I got the following error message at compile time: Friend assembly reference ‘UnitTests’ is invalid. Strong-name signed assemblies must specify a public key token in their InternalsVisibleTo declarations. So what to do about this? Well, first the UnitTests assembly needs to be signed with a strong name, too. Then the tricky part is using the correct string to pass to the InternalsVisibleTo attribute. Problem is, you won’t be able to compile either of the two assemblies before you get that syntax right: the UnitTests assembly doesn’t compile because it references the other assembly, which can’t be compiled because the attribute is not correctly configured. The trick is to find out the public key token of the strong name key pair you are using to sign the UnitTests assembly. There are several ways to do that:

Many things change with the decision to work with purely object-oriented data in a specific situation. The outlook seems good: business processes and rules will be much easier to implement, completely typed data will be no problem at all and there’ll be no more structural problems trying to accomodate clumsy handling of records and rows in an otherwise OO application structure. There’ll be an object/relational mapping tool that takes care of all the persistence issues. There’s one thing though that will pose problems much greater than originally anticipated, and it’s easy to overlook large parts of that in the original decision: the wide topic of data integrity in the object world (OW). I’m going to present some general questions and theories about data integrity in conjunction with OO data objects in this article and I’m planning to write some further articles on the same topic later. Occasionally I may reference the technology I’m personally using at the moment, which is .NET 2, the C# language and XPO.

Data integrity, what about it?

Alright, consider the following situation: There’s a data class with two properties, Index and Content. There’s a specialised collection typed to contain instances of the data class. Something like this:

public class DataClass {
  public DataClass(string index, string content) {
    this.index = index;
    this.content = content;
  }

  string index;
  public string Index { get { return index; } }

  string content;
  public string Content { get { return content; } }
}

public class MyCollection : List<DataClass> {
}

In this post I showed how nullable types can be simulated in .NET 1, especially for use with XPO. Prompted by Miha Markic, I had a look at the possibility of data binding with those simulated nullable types. I found that while the implementation I had suggested could be used without problems in a read-only situation, it obviously didn’t contain any logic that would enable editing the types via data-bound controls. Nevertheless, this is easy to do by creating a custom TypeConverter. Like this:

public class NullableInt32TypeConverter : TypeConverter {
  public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {
    if (sourceType == typeof(string))
      return true;
    return base.CanConvertFrom(context, sourceType);
  }

  public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) {
    if (value is string) {
      try {
        int intVal = Convert.ToInt32(value);
        return new NullableInt32(intVal);
      }
      catch (FormatException) {
        return new NullableInt32( );
      }
    }

    return base.ConvertFrom(context, culture, value);
  }
}