How to identify static class using reflection


Today someone asked me, how to find static class from list of types. I was against the implementation he was doing, but still I couldn’t find how to find static class. Later I did some research and I found like you need to look for IsAbstract and IsSealed properties of the type.

Here is source code, which identify the static class from the list of available types in the assembly.

class Program
    static void Main(string[] args)
        var types = Assembly.GetExecutingAssembly().GetTypes();
        types.Where(x => x.IsAbstract && x.IsSealed).ToList()

static class StaticClass { }

sealed class SealedClass { }

interface Interface { }

class NormalClass { }

abstract class AbstractClass { }

If you run the above code, it will print the StaticClass name.

Happy Programming :)

Introduction to Dapper.NET ORM in ASP.NET 5


Dapper.NET is an open source, high performance Micro-ORM supporting Sql Server, MySQL, Sqlite, SqlCE, Firebird etc, from Stack Overflow team. Drapper provides extension methods for IDbConnection, so you require an instance of IDbConnection to use Dapper. To use Dapper, you need to include Dapper in the project.json file. In this post I am using Repository pattern and injecting the repository to the controller via constructor injection.

Here is the project.json file, the EntityFramework.SqlServer reference is required to use IDbConnection interface.

  "dependencies": {
    "Microsoft.AspNet.Diagnostics": "1.0.0-beta2",
    "Microsoft.AspNet.Hosting": "1.0.0-beta2",
    "Microsoft.AspNet.Mvc": "6.0.0-beta2",
    "Microsoft.AspNet.Server.WebListener": "1.0.0-beta2",
    "EntityFramework.SqlServer": "7.0.0-beta2",
    "Microsoft.AspNet.StaticFiles": "1.0.0-beta2",
  "commands": {
    "web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5010",
  "frameworks": {
    "aspnet50": {}

Here is the constructor of the respository class. In this I am creating the instance of SqlConnection.

private IDbConnection _connection;
public EmployeeRepository()
	_connection = new SqlConnection("Server=.\\SQLEXPRESS; Database=DemoDb; Integrated Security=SSPI");

Dapper extends the IDbConnection with Query method, which you can use to execute a query and map the result to a strongly typed object or list of objects.

public Employee Get(int? id)
	return _connection.Query<Employee>("SELECT * FROM Employees WHERE Id = @Id", new{ Id = id }).FirstOrDefault();

public IEnumerable<Employee> FindAll()
	return _connection.Query<Employee>("SELECT * FROM Employees");

You can use Execute method to execute DML queries as well to the Database.

public void Save(Employee employee)
	_connection.Execute("INSERT INTO Employees(Name,Designation,JoiningDate,Remarks) VALUES(@Name,@Designation,@JoiningDate,@Remarks)", employee);

public void Delete(Employee employee)
	_connection.Execute("DELETE FROM Employees WHERE Id=@Id", employee);

Dapper also supports Stored Procedures as well. You need to provide the command type parameter.

public void Update(Employee employee)
	_connection.Query("usp_UpdateEmployee", employee, commandType: CommandType.StoredProcedure);

You can use Dapper in ASP.NET or any .net applications, for that you need to include the Dapper nuget reference. You can find more details about Dapper from Dapper wiki

Happy Programming :)

How Intercept HTTP calls from WCF client to service


Today I faced some problem, I had to intercept HTTP calls from WCF proxy to WCF service. I was using Fiddler 4. But unfortunely it was not working :( I tried many options.(It was not working for me, but it may work for you)

  1. By using proxy setting – In the clientapp.exe.config, you need to add following configuration settings.
      <defaultProxy enabled="true">
        <proxy proxyaddress="" bypassonlocal="False" />
  2. By using useDefaultWebProxy attribute in the binding element.
        <binding name="BasicHttpBinding_IService1" 
                 useDefaultWebProxy="false" />

    For me it is also not working :(

  3. By using Hostname instead of localhost. – This is the only solution worked for me. :) Modify you clientapp.exe.config, and change the endpoint address from localhost to the actual host name.
      <endpoint address="http://anurajp/WcfService1/Service1.svc" 
          name="BasicHttpBinding_IService1" />

    This is the proxy request and response captured using Fiddler.

    WCF Request and Response using Fiddler

    WCF Request and Response using Fiddler

    Happy Programming :)

Converting DataTable to Generic List<T>


Code snippet for converting a DataTable to Generic List<T>.

public static List<TEntity> ToList<TEntity>(this DataTable dataTable,
    Dictionary<string, string> mapping) where TEntity : new()
    var list = new List<TEntity>();
    foreach (DataRow dataRow in dataTable.Rows)
        var entity = new TEntity();
        foreach (var keyvaluePair in mapping)
            var property = entity.GetType().GetProperty(keyvaluePair.Key,
                            BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
            if (dataRow[keyvaluePair.Value] != DBNull.Value)
                var propertyType = 
                    Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType;
                var value = 
                    Convert.ChangeType(dataRow[keyvaluePair.Value], propertyType);
                property.SetValue(entity, value, null);


    return list;

You can use this as an extension method to the Data Table class. By providing the mapping dictionary, the consumer class can decide which all columns / properties need to participate in the conversion.

Happy Programming :)