Monday, 24 June 2013

Working With Generics

working with generics

create a class-->>declare the properties/variable-->>with in the class create a Method with inherited one(here it will be inherited from the Interface{Class:Interface})


public class sample:ICreatable
{
  public string ramu {get;set;}

   public void Create(SqlDataReader Reader)
   {
    ramu=Convert.ToString(Reader["DBname"]);
   }
}

/*------this is the list-------*/

List<sample> sampledata = new List<sample>();


public class DAGetList
{
    public static List<T> GetListFromCommand<T>(SqlCommand sqlcmd)
    where T : ICreatable, new()
    {
        List<T> results = new List<T>();
        using (sqlcmd.Connection)
        {
            sqlcmd.Connection.Open();
            SqlDataReader sdr = sqlcmd.ExecuteReader();
            while (sdr.Read())
            {
                T newthing = new T();
                newthing.Create(sdr);
                results.Add(newthing);
            }
        }
        return results;
    }
}

public interface ICreatable
{
    void Create(SqlDataReader reader);
}

No comments:

Post a Comment

Extracting Nupkg files using command line

Rename it to zip first then extract files as below 1.     Rename-Item -Path A_Package.nupkg -NewName A_Package.zip 2.     Make sure to repla...