Reflection in .Net with a C# example Monday, Jul 18 2011 

Reflection is the ability of the .NET framework to gather information (metadata) about assemblies, modules and types at runtime. It allows you also to dynamically create instances of types, invoke methods and access fields, properties and attributes.

Reflection is an important capability of the .NET framework and enables you to get information about objects at runtime. In this article, we will iterate through all properties for a class named Person using reflection.

using System;
using System.Reflection;

class Program
{
static void Main(string[] args)
{
Person person = new Person();
person.Age = 27;
person.Name = “Fernando Vezzali”;

Type type = typeof(Person);
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo property in properties)
{
Console.WriteLine(“{0} = {1}”, property.Name, property.GetValue(person, null));
}

Console.Read();
}
}

The Person class extends System.Object and does not implement any interface. It is a simple class used to store information about a person:

class Person
{
private int age;
private string name;

public int Age
{
get { return age; }
set { age = value; }
}

public string Name
{
get { return name; }
set { name = value; }
}
}

 

Advertisements

Common SELECT Statements (SQL Server) Monday, Jul 18 2011 

Followings are some examples of using SELECT statement. My table name tblTest. Followings are columns. (TestID, TestName, TestDate, TestCity )

1) Select * from tblTest (Returns all columns/Rows)

2) Select * from tblTest Where TestID=2 (Returns the row/s which TestID has value 2)

3) Select * from tblTest where TestID Between 10 and 20 (Return all rows between 10 and 20, this result includes 10 and 20)

4) Select * from tblTest Where TestCity in (‘New York’,’Washington’,’California’) (Returns all rows which city is NewYork, Washington, california)

5) Select * from tblTest Where TestName Like ‘A%’ (Return all rows where the name starts letter A)

6) Select * from tblTest Where TestName Like ‘%A’ (Return all rows where the name ends letter A)

7) Select * from tblTest Where TestName Like ‘[ABC]%’ (Return all rows of name start with A / B / C)

8) Select * from tblTest Where TestName Like ‘[^ABC]%’ (Return all rows of name not start with A and B and C)

9) Select (TestName+space(1)+TestCity) as Address from tblTest (Returns single column address, name and city added together with a space)

10) Select * from tblTest Where TestName IS NULL (Return all rows which TestNane has null values)

11) Select * from tblTest Where TestName IS NOT NULL (Return all rows which TestNane has not null values)

12) Select * from tblTest Order By TestID Desc (Sort the result set descending order, Asc or not using any sort Ascending order)

13) Select ‘Visual Studio’ as IDE, ‘2010’ as Version (Creating memory resident result set with two columns[IDE and Version])

14) Select Distinct TestID from tblTest (Returns unique rows based on TestID)

15) Select Top 10 * from tblTest  (Return 10 customers randomly)

16) Select getdate() (Shows the current date)

17) Select db_name() (shows the database name which you are working on)

18) Select @@Servername (Shows name of the server)

19) Select serverproperty (‘Edition’) (You can pass following ServerName, Edition, EngineEdition, ProductLevel to get current information about the server)

20) Select user_name() (Get current user)

21)  Select *  into #test from tblTest  (Create temporary table #test and insert all records from tblTest)

22)  Select Max(TestID) from tblTest (Returns Maximum TestID from tblTest)

23)  Select * from tblTest Compute Max(TestID) (Returns two result sets – getting all rows and maximum value of TestID)

24) Select FirstName, LastName,  Salary, DOB,

Case Gender
When ‘M’ Then ‘Male’
When ‘F’ Then ‘Female’
End
From Employees

(This Change Gender fields as if M then prints Male and if F then prints Female)