Friday, May 23, 2008

Invoke method with ref and out parameters

The other day I was tryin to load an assembly using .NET Reflection and invoke a method from a given class that had an ref parameter.

After a Google-search I've found this article : http://iamacamera.org/default.aspx?section=home&id=62.

In this article, the author used basic .NET types (common types) like int32 and string. But what if I have an enum that is Int32 or some other custom class?... As you can see in the article, the key is to load the "System.String&" type and get the method using the obj.GetMethod(arguments, types);

If you'll try the following code, will not work:

Type.GetType(typeof(MyNameSpace.MyClass).FullName + "&");
Type.GetType("MyNameSpace.MyClass&");

But there is a solution...

I've made a generic method that I can use over and over again... It looks like this...

public static void InvokeMethod(string assemblyName, string className, string methodName, out object result, ref object[] arguments)
{

Assembly assembly = Assembly.LoadFrom(assemblyName);
// Walk through each type in the assembly
foreach (Type type in assembly.GetTypes())
{
// Pick up a class
if (type.IsClass == true && type.Name == className)
{
// Dynamically Invoke the Object
MethodInfo info = type.GetMethod(methodName);
ParameterInfo[] pars = info.GetParameters();
Type[] types = new Type[arguments.Length];
for (int i = 0; i < types.Length; ++i)
types[i] = pars[i].ParameterType;
info = type.GetMethod(methodName, types);
result = info.Invoke(null, arguments);
return;
}
}

return null;
}
For my need, MyClass was a static class and that's why I used info.Invoke(null, arguments);... To create an instance you have to user the Activator class like this...

object ibaseObject = Activator.CreateInstance(type);
result = info.Invoke(ibaseObject, arguments);

As you can see I'm fooling .NET and make him think I can load a refrence type like "MyNameSpce.MyClass&". The solution here was the use of ParameterInfo class from where I can get the REAL runtime type of an parameter.

Hope this helps someone some day... it sure helped me...:D

Monday, April 21, 2008

T-SQL datalength (image size or length)

Sometimes you have to insert into a database some binary information(like images). The MS SQL data type for this kind of data is image. Very often you want to know the exact size or length of the data you have in that comlumn.

The SQL function that returns the "number of bytes used to represent any expression" is DATALENGTH ( expression ) .

As an example:
SELECT DATALENGTH(MyImage), ImageID
FROM MyImagesTable

Sunday, April 20, 2008

Creation of Joukowski airfoils

One of the most nice and usefull airfoils constructions is the creation of the Joukowski airfoils. This construction uses conformal map theory.

The airfoil obtained with this method is also named Joukowski-Ceaplîghin airfoil.

The method is described below:


To obtain a Joukowski airfoil, with one face concave, we take a circle K, with the center in the first dial and let K’ be the transformed circle obtained using the transformation - the circle inversion with the following formula:
We notice that the upper transformation is a geometric sum of two transformations : (the circle K is transforme in itself), and , a inversion. The last transformation will rezult into a circle K' that intersects the Ox axis in a point B(-l,0), because the inverse is . If we draw the simetric radius OP and OP’, P on the base circle K and P’ on the transformed circle, the point P’’ on the airfoil, is obtained making the vectorial sum . These kind of airfils are used as the base airfoils for the aircrafts wings.

The following source code generates a airfoil in Matlab.

function profil = draw_airfoil(ox,oy,rho)
%point index
persistent index;
if isempty(index)==1
index = 1;
end

%transform factor-the intersection of the base circle with Ox
l=abs(ox-sqrt(rho^2-oy^2));

%obtinere cerc de baza
theta = 0:0.1:360;
theta=theta*pi/180;
px = ox + R*cos(theta);
py = oy + R*sin(theta);

cla;
grid on;
box on;
axis equal;
plot(px,py);%base circle
plot(px(index),py(index),'--bs','LineWidth',2,...
'MarkerEdgeColor','k',...
'MarkerFaceColor','b',...
'MarkerSize',10); %point on the circle
line('XData', [0 px(index)], 'YData', [0 py(index)], 'LineStyle', '--');

hold on;

%circle inversion
z=px+i*py;
csi = (l^2)./z;
plot(real(csi), imag(csi), 'Color','red'); % transformed circle
plot(real(csi(index)),imag(csi(index)),'--rs','LineWidth',2,...
'MarkerEdgeColor','k',...
'MarkerFaceColor','r',...
'MarkerSize',10);%point on the inverse circle
line('XData', [0 real(csi(index))], 'YData', [0 imag(csi(index))], 'LineStyle', '--');

hold on;
if index >= length(z)
index = 1;
end
%the resulted airfoil
plot(real(z(1:index))+real(csi(1:index)),imag(z(1:index))+imag(csi(1:index)), 'Color', 'green');
plot(real(z(index))+real(csi(index)),imag(z(index))+imag(csi(index)),'--gs','LineWidth',2,...
'MarkerEdgeColor','k',...
'MarkerFaceColor','g',...
'MarkerSize',10);
line('XData', [real(z(index))+real(csi(index)) real(z(index))], 'YData', [imag(z(index))+imag(csi(index)) imag(z(index))], 'LineStyle', '--');
line('XData', [real(z(index))+real(csi(index)) real(csi(index))], 'YData', [imag(z(index))+imag(csi(index)) imag(csi(index))], 'LineStyle', '--');

legend('Base circle','Point on the base circle','Line 1',...
'Inverted circle', 'Point on the inverted circle', 'Line 2',...
'Airfoil', 'Point on the airfoil');

index=index+50;


The output of this code is :