BeFruit

Tuesday, July 22, 2008

Clone objects in C#

It is simple to clone objects (i.e. make an exact copy of an object) that implement the IClonable interface. It has the Clone() method that you just have to call.

When the method is not available and the class is serializable (has the Serializable attribute), it is possible to use this feature. There are some examples over the Internet, here is a variant I adapted as a templated method:
private static T Clone<T>(T original)
{
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, original);
ms.Flush();
ms.Position = 0;
return (T)bf.Deserialize(ms);
}

You call it this way:
MyClass copy = Clone<MyClass>(original);


But what to do with a non serializable object? The only way is to use reflection. Here is a simplified but working example. One should take care of readable and writable properties.
private static T Clone<T>(T original)
{
T copy = (T)(typeof(T).GetConstructor(System.Type.EmptyTypes).Invoke(null));
foreach (PropertyInfo pi in typeof(T).GetProperties())
{
object originalValue = pi.GetValue(original, null);
pi.SetValue(copy, originalValue, null);
}
return copy;
}

Labels:

Friday, March 21, 2008

COMException when opening WebApplication project

I recently go the following error while opening a Visual Studio solution: "System.Runtime.InteropServices.COMException". Not very explicit, is it?

I found a page in VisualStudio feedback explaining that this is a bug in Visual Studio 2008, that should be corrected "in the next release"...

The explanation is that VS is trying to locate your Web Application in IIS. Look into your project file *.csproj in the <WebProjectProperties> section, the searched URL is indicated in the <IISUrl> tag.

The solution is simple: in the <UseIIS> tag change "True" to "False" and your project will open like a charm.

Labels: ,

Tuesday, May 29, 2007

A bridge between JavaScript and C#: AjaxPro

It is now very common to JavaScript programmers to call remote procedures on the server, either directly using the XMLHttpRequest object or a framework ensuring cross-browser compatibility.
What is then missing is the server-side machinery able to answer the request. You have to parse the request parameters, process data and build a return value as a string or an XML document. It is relatively simple as long as you are using basic types, but gets complicated if you want to pass a custom object or a DataGrid.
If you are lucky enough to be using .NET, here comes AjaxPro. You create an .aspx page with a public class MyClass. Add a public method MyMethod with the attribute [AjaxPro.AjaxMethod] and that's all. You are now able to call your remote method from JavaScript MyClass.MyMethod(...) just like if it was locally on client-side.

The best part for me is the following: the AjaxPro project manager is planning to make it compatible with jQuery, i.e. make AjaxPro generate JavaScript proxies with the jQuery framework. This will lead the ease of programming and abstraction level one step further.

Labels: , ,