20 February, 2009, 16:03
Multiple Inheritance is a feature that a class that can inherit more than one class. Although CLR does not really care about it, it is not possible in the mainstream .NET languages to have multiple inheritance.
It is arguable that why we would ever need multiple inheritance. I think it is the same paradox as having dynamic types in a statically typed language. I am not going into that debate as it is already decided for almost a decade. This is an attempt on how to achieve the same effect using dynamic types easily.
In C++, multiple inheritance can simply be expressed like this :
class Auto {
public:
virtual void Go(int a) {}
virtual ~Auto() {}
};
class Animal {
public:
virtual void Go() {}
virtual ~Animal() {}
};
class SampleClass : public Auto, public Animal{};
int main() {
SampleClass *a = new SampleClass();
//a->Go(5); // Compiler error for ambigouity
//a->Go("going"); // Compiler error for missing method
Animal* an = dynamic_cast<Animal*>(a);
an->Go();
Auto* at = dynamic_cast<Auto*>(a);
at->Go(7);
}
When C# introduced dynamic type system and shared type system with C# 4, I thought it should be possible to express multiple inheritance using the new syntax. More like a forced Javascript prototypal inheritance instead of classical inheritance. However it turned out that it was even easier than that. It is relying on the runtime to operate on the types underlying with dynamic conversions, simply reflection.
Obviously it is not forced by the compiler, and you wouldn’t get intellisense but it will behave like a multiple inherited object. To achieve that the dynamic features of C# 4 has been used.
The same implementation in C# using Minherit object that has been created for reflected multiple inheritance.
So in C#, it would look like this :
public class Auto
{
public string Go(int a) {}
}
public class Animal
{
public string Go() {}
}
public class SampleClass : Minherit<Auto, Animal>
{
static void Main(string[] args)
{
dynamic sample = new SampleClass();
a.Go(4); // this will succeed at runtime and call the first Go(int)
a.Go("going"); // runtime error for missing method
Animal an = (Animal)sample;
Auto x = (Auto)sample;
an.Go();
x.Go(7);
}
}
It uses dynamic sample that has come with .NET Framework 4 CTP. I actually think that sample will go to mscorlib with the final release in the System namespace
As the code got a bit long, I post it as project zip file if you are interested.
Downloads: 389 File Name: cs4trials.zip
It is possible to create multiple inheritance abstraction as shown below. Here are the selected four methods to display how the GetMember and SetMember is implemented.
public override object Call(CallAction action, params object[] args)
{
var objMethod = GetMember<MethodInfo>(this, action.Name).Concat(
GetMember<MethodInfo>(base1, action.Name)).Concat(
GetMember<MethodInfo>(base2, action.Name));
object result = null;
bool executionSuccess = false;
foreach (var item in objMethod)
{
try
{
result = item.Second.Invoke(item.First, args);
executionSuccess = true;
break;
}
catch
{
}
}
if (executionSuccess)
{
return result;
}
else
{
throw new MissingMethodException(action.Name);
}
}
private IEnumerable<Pair<object, T>> GetMember<T>(object obj, string name)
{
return from member in obj.GetType().GetMember(name).OfType<T>()
select new Pair<object, T>(obj, member);
}
public override object GetMember(GetMemberAction action)
{
var property = GetMember<PropertyInfo>(this, action.Name).Concat(
GetMember<PropertyInfo>(base1, action.Name)).Concat(
GetMember<PropertyInfo>(base2, action.Name)).FirstOrDefault();
var field = GetMember<FieldInfo>(this, action.Name).Concat(
GetMember<FieldInfo>(base1, action.Name)).Concat(
GetMember<FieldInfo>(base2, action.Name)).FirstOrDefault();
if (property != null)
{
return property.Second.GetValue(property.First, null);
}
else if (field != null)
{
return field.Second.GetValue(field.First);
}
else
{
throw new MissingMemberException(action.Name);
}
}
public override void SetMember(SetMemberAction action, object value)
{
var properties = GetMember<PropertyInfo>(this, action.Name).Concat(
GetMember<PropertyInfo>(base1, action.Name)).Concat(
GetMember<PropertyInfo>(base2, action.Name));
var fields = GetMember<FieldInfo>(this, action.Name).Concat(
GetMember<FieldInfo>(base1, action.Name)).Concat(
GetMember<FieldInfo>(base2, action.Name));
bool issucceess = false;
foreach (var property in properties)
{
try
{
property.Second.SetValue(property.First, value, null);
issucceess = true;
break;
}
catch { }
}
if (!issucceess)
{
foreach (var field in fields)
{
try
{
field.Second.SetValue(field.First, value);
issucceess = true;
break;
}
catch { }
}
}
if (!issucceess)
{
throw new MissingMemberException(action.Name);
}
}
20 July, 2008, 00:50
I was using Eclipse for one for the previous projects as a development environment. It has some powerful features. But I have to say not as powerful as Visual Studio. Anyway since it is possibly the best IDE for Java development it was my choice on the day of implementation.
The project that I was working was done using Borland’s Jbuilder. Unfortunately that IDE doesn’t exist anymore as it is discontinued by a new company. Thanks to Eclipse import project wizard, the project migration wasn’t difficult at all.
I just opened the project XML and the project was ready to be working. However, when using with different big open source libraries, Eclipse has a problem in managing them. Actually I have a problem in managing them, because I need to find the required version of each dependency and so on. It is the difficulty of package and build management of eclipse.
It is often difficult to reference a library and get all the dependencies on Eclipse. Somehow it doesn’t warn at compile time as well and you get exception at runtime which is not nice enough.
Than I found a project called Maven for doing proper build systems. What it does is has an online repository that have all the dependencies filed and it downloads the required assemblies for your project. So you don’t have download each package separately and link to project. All the best it is very well integrated with eclipse with its marvelous plug-in.
You load the plug-in and tell maven to add a reference to your project. I know Java programmers call this as something else but since I’m mainly a .NET programmer I hope somebody will find it useful.
Anyway, it stores the configuration in XML file that you can tweak it later if needed. All the updates to the references can happen automatically as well which is quite nice.
20 July, 2008, 00:23
Last year, Windows Kernel team released a new cryptography libraries called Next Generation Cryptography (CNG). Although it is only available on Windows Vista and Windows Server 2008 kernels so far, I think it is going to replace the CAPICOM with its powerful new implementations.
I quite liked the new CNG libraries as it gets over some limitations of the previous library.
CLR security team released a new project on codeplex called CLR Security for that new cryptography library on .NET. So it is possible to benefit from those libraries in managed worlds as well. For a detailed description be sure to check security blog.
Also, the team is very quick to respond and provide fixes if necessary.
19 July, 2008, 23:43

I was thinking of reading a Lisp book. I checked the titles and decided to buy,
luckily, it’s freely provided in the Apress’ Practical Common Lisp page.
I doubt that it is done by mistake, because the e-book price is also indicated but anyway there is a free link to download it.
9 April, 2008, 01:11
I was working with C++ Standard Template Library (STL) library and when using the StringStream class I felt like we need this for .NET
Yes we have a great class StringBuilder but we can’t use it as a Stream (because it is not).
Most of the cryptographic classes works with the stream object. I wanted to save the encrypted information as text. Ideally we could use memoryStream but it is not really efficient, because it is bits and we need to build a model for serializing/deserializing as text.
String Stream class (as I call it) is just an ugly but functional wrapper around StringBuilder class as shown some snippets below. I just wanted it to use with encryption and decryption a string. So only the functions needed is there which is enough to use. But I doubt it leaks a little, so use with care if needed.
public class StringStream : Stream
{
private StringBuilder strBuilder;
public StringStream()
{ strBuilder = new StringBuilder(); }
public StringStream(string str)
{ strBuilder = new StringBuilder(str); }
public override int Read(byte[] buffer, int offset, int count)
{
int howMuchRead = 0;
for (int i = offset; i < count; i++)
{
var actualIndex = i * 2;
howMuchRead = i+1;
if (actualIndex >= strBuilder.Length)
{
howMuchRead = count;
break;
}
string s = strBuilder[actualIndex].ToString() + strBuilder[actualIndex + 1].ToString();
buffer[i] = Convert.ToByte(s,16);
}
return howMuchRead;
}
public override void Write(byte[] buffer, int offset, int count)
{
for (int i = offset; i < count; i++)
{
strBuilder.Append(buffer[i].ToString("x2"));
}
}
}
Downloads: 263 File Name: StringStream.cs
The main magic is happening on reading and writing. each byte is represented as 2 characters in the string. It is using the helper function Convert.ToByte to make it happen.
In the next post I will use it with a symmetric encryption helper class.
9 March, 2008, 18:26
After reading interesting posts about the fonts and colors in Visual Studio, I gave a try to some of them. Because I work quite a lot with Visual Studio, I realized that it is actually very important for the eyes and for productivity. Unfortunately none of the themes have pleased my IDE concept, low contrast, dark background, pale colours, clear type fonts, and follow metaphors.
I also put some time to design and experiment with some fonts and colours. The themes that I found were to dark for me with very high contrast, and the fonts were glowing.
This theme is still dark but with pale colours. More importantly it follows the metaphors we used to, so it will be OK when discussing with developers. What I mean? You know greenish parts of your code are always comments, or red parts of SQL code is bad (strings for dynamic SQL), as we all used to from visual studio, SQL or any popular IDEs.
One more thing is that when working with F# some of the color features are not the same as C#. So this theme is prepared with the F# projects in mind as well. Also HTML, CSS, JavaScript, and XAML are formatted following my colour preference.
For fonts I have used DejaVu font with size 10. If Consolas is your favourite font than, it would be better to put on size 9.
Moreover if you use, ReSharper code refactoring tool it has also some colour highlights for it as well. But it is not a problem if you don’t.
Downloads: 138 File Name: Dark_IDE_2010.zip
Downloads: 1650 File Name: Friendly_IDE_2008.zip
Downloads: 596 File Name: Friendly_IDE_2005.zip
C# – F# Scheme
ASPX – HTML – XML Scheme
