Remoting Configuration

When using .Net Remoting and configuring system through configuration file, you usually will find code like this:
 

RemotingConfiguration.Configure(“validate.config”);  

but if you dont want to hard code the name of the configuration file and want to use the default config file of your application. How you can achieve this. Well it caused me a lot of time to find it but finally it is really easy. Simply use the following code:

RemotingConfiguration.Configure(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

 

    

 

How to create a VB Script Object and call methods on it

Well you will be surprised to know that it is very easy to create a VB Script object in C# and call methods on it. Consider the following VB Script Code:
 
Dim IIsWebServiceObj
Set IIsWebServiceObj = GetObject("IIS://localhost/W3SVC")
 
IIsWebServiceObj.DeleteExtensionFileRecord "E:\RMS\Code\BusinessServices\KalSPS\Debug\KalSPS.dll"
 
To write this code in C#, you just need to add a reference to Microsoft.VisualBasic.CompilerServices and System.Runtime.CompilerServices. The following code snippet creates an IIS object and call DeleteExtensionRecord Method on it.
 

private

void RemovesWebServiceExtension(string extensionPath)

{

try

{

object ext = this.GetIISObject("IIS://" + Environment.MachineName+"/W3SVC");

object[] args=new object[1]{extensionPath};

LateBinding.LateCall(ext, null, "DeleteExtensionFileRecord", args, null, null);

}

catch

{

throw;

}

}

private object GetIISObject(string fullObjectPath)

{

try

{

return RuntimeHelpers.GetObjectValue(Interaction.GetObject(fullObjectPath, null));

}

catch

{

throw;

}

}

 

where Interaction module contains procedures to interact with objects, applications and system. Runtime Helper class contains method for compiler construction. It is a service class and hence contains only static methods. Its GetObjectValue method boxes the object passed as the parameter. Boxing a value type creates an object and performs a shallow copy of the fields of the specified value type into the new object. Finally LateBinding is used as an object of type Object can be bound to object of any type i.e. it behaves as a varient type.
 
The first argument of LateBinding.LateCall method is the object on which the method should be called, second argument is the type of the object i.e. System.Type object, the third argument is the name of the method, 4th is set of arguments to be passed to that method, 5th argument is the set of names of the parameters to be passed to that method if they have any, and finally 6th argument is the set of booleans indicating whether to copy back any parameters.
Tell me isnt it very simple

How to find the path of the layouts folder of SPS using IIS interface

It looks as it is very difficult to programmatically find the layouts folder of SPS using IIS object model but i found it very easy and interesting using directoryservices and I am sure you will have the same opinion.
 

private string FindSPSPath( string spsPath )

{

try

{

string metabasePath="IIS://Localhost/W3SVC";

DirectoryEntry path =

new DirectoryEntry(metabasePath);

PropertyCollection properties=path.Properties;

DirectoryEntries entries=path.Children;

foreach(DirectoryEntry entry in entries)

{

if( entry.SchemaClassName=="IIsWebServer" && entry.Properties["ServerComment"][0].ToString().ToLower().Equals(spsPath.ToLower()) )

{

DirectoryEntries vdirs = entry.Children;

foreach(DirectoryEntry vdir in vdirs)

if( vdir.Name== "root")

{

DirectoryEntries childVdirs = vdir.Children;

foreach(DirectoryEntry childVdir in childVdirs)

if(childVdir.Name=="_layouts")

return childVdir.Properties["Path"].Value.ToString();

}

}

}

return null;

}

catch

{

throw;

}

}

 

Here what I have done is to traverse through the collection of children of the IIS Server i.e. IIsWebService. It contains all the Web Servers and Application Pools indicated by a number n following the path IIS://Localhost/W3SVC/n. The next step is to find the appropriate Web Server you are looking for which can be found through the ServerComment property of the Web Server. Also dont forget to check the type of the child of IIS as it can be application pool and application pools dont have such property. The SchemaClassName of the Web Servers is IIsWebServer.

 

Now we have found the Web Server of the SPS (SharePoint Portal Server). The next step is to find the layouts virtual directory of SPS. Here is something that is typical to SPS. The SPS conatins 2 virtual directories which are not visible through inetmgr that are root and filters. root is the virtual directory that we are interested in as it contains all the rest virtual directories including _vti_bin, layouts etc. So we have now reached to layouts virtual directory of SPS. The final step is to find its physical path. The Path property of virtual directories contain their physical path. In the similar fashion you can find any property of any virtual directory or can modify them. 

How to handle New Row in the Grid Control

Recently I was developing some grid controls where I faced a situation where I want to programtically update the cell of a newly created row on its cell changed. The status of a New Row in the datagrid is always Detached until you changed the focus from that row to some other row, when its status changed to added. If you try to change the value of the cell in the row when its status is detached, it will not work and if you try to remove that row from its source and then add it, it will give error whenever you try to change its value that the row has already been added.
 
So I used the following code for the detached row and it worked:
 

//If it is a new row that has just been added but not attached to data grid, then ends its editing.

if(dr.RowState == DataRowState.Detached )

this.BindingContext[this.grid.DataSource, this.grid.DataMember].EndCurrentEdit();

A generic Equal Method

Recently I faced a situation where I want to write an override of Equal method so that I can compare any 2 of my business objects. So the best way to do is to override the Equals and GetHashCode method in the base class for all business objects. The following code is a very simple way to compare any 2 objects:
 

public override bool Equals(object obj)

{

PropertyInfo [] properties=this.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

foreach( PropertyInfo property in properties )

{

object obj1=property.GetValue(this, null), obj2=property.GetValue(obj, null);

if( (obj1==null && obj2!=null) || (obj1!=null && obj2==null) ||

((obj1!=

null && obj2!=null) && !obj1.Equals(obj2)))

return false;

}

return true;

}

public override int GetHashCode()

{

return base.GetHashCode ();

}

Copy constructor for child object

Many times we faced the situation where we want to do something like this:
 
A a=new B();
 
Where A is the child object and B is the parent which is not possible. We can achieve this goal by copy constructor like this:
 

public BaseEntity(ref object parent):this()

{

if(parent==null)

return;

FieldInfo [] fields;

if(parent.GetType()==this.GetType().BaseType)

fields=parent.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

else

fields=

this.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

foreach(FieldInfo field in fields)

{

object val=field.GetValue(parent);

field.SetValue(

this, val);

}

}