How to loop through object’s properties ?

If you are looking to loop through all properties of an object then this article may be helpful for you.

The .NET Framework provides two ways to access metadata on a type:

1) Reflection API provided in the System.Reflection namespace

2) TypeDescriptor class

Folowing is the sample code for TypeDescriptor class.

	public class EmployeeClass
        [Description("Gives/Store employee first name")]
        public string FirstName { get; set; }

        [Description("Gives/Store employee last name")]
        public string LastName { get; set; }

        [Description("Gives/Store employee gender info")]
        public string Gender { get; set; }

        [Description("Gives/Store employee age")]
        public int Age { get; set; }
    EmployeeClass objEmployee = new EmployeeClass()
		FirstName = "Jogendra",
		LastName = "Singh",
		Gender = "Male",
		Age = 28

	PropertyDescriptorCollection pdCollection = TypeDescriptor.GetProperties(objEmployee);

	foreach (PropertyDescriptor property in pdCollection)
		object propDisplayName = property.DisplayName;                
		object propDesc = property.Description;
		object propValue = property.GetValue(objEmployee);

For more info please visit to :-

Programming is Easy…


How to call a Stored Procedure in Code First Entity Framework

	using (var _dbContext = new TestDbContext())
		//Open Database connection
		//Create command object
		DbCommand _cmd = _dbContext.Database.Connection.CreateCommand();
		//Set procedure name as command text
		_cmd.CommandText = "SPName";
		//Set command type to stored procedure
		_cmd.CommandType = CommandType.StoredProcedure;
                // Set input parameter value
		_cmd.Parameters.Add(new SqlParameter("@Id", 100));

                //Set Out put parameter value
		var _resultParam = new SqlParameter("@Result", SqlDbType.NVarChar, 1000)
                { Direction = ParameterDirection.Output };

	    return _strResult = _resultParam.Value != null ? _resultParam.Value.ToString() 
                                : "Error Message!!!";

Programming is Easy…

Use of Conditional attribute in C#

Makes the execution of a method dependent on a preprocessing identifier. The Conditional attribute is an alias for ConditionalAttribute, and can be applied to a method or an attribute class.

In this example, Conditional is applied to a method to enable or disable the display of program-specific diagnostic information:

#define TRACE_ON
using System;
using System.Diagnostics;

public class Trace
    public static void Msg(string msg)

public class ProgramClass
    static void Main()
        Trace.Msg("Now in Main...");


f the TRACE_ON identifier is not defined, no trace output will be displayed.

The Conditional attribute is often used with the DEBUG identifier to enable trace and logging features for debug builds but not in release builds, like this:

static void DebugMethod()

When a method marked as conditional is called, the presence or absence of the specified preprocessing symbol determines whether the call is included or omitted. If the symbol is defined, the call is included; otherwise, the call is omitted. Using Conditional is a cleaner, more elegant, and less error-prone alternative to enclosing methods inside #if and #endif, like this:

void ConditionalMethod()

A conditional method must be a method in a class or struct declaration and must have a return type of void.

Using Multiple Identifiers :-

If a method has multiple Conditional attributes, a call to the method is included if at least one of the conditional symbols is defined (in other words, the symbols are logically ORed together). In this example, the presence of either A or B will result in a method call:

[Conditional("A"), Conditional("B")]
static void DoIfAorB()
    // ...

To achieve the effect of logically ANDing symbols, you can define serial conditional methods. For example, the second method below will execute only if both A and B are defined:

static void DoIfA()

static void DoIfAandB()
    // Code to execute when both A and B are defined...

Using conditional with Attributes class :-

The Conditional attribute can also be applied to an attribute class definition. In this example, the custom attribute Documentation will only add information to the metadata if DEBUG is defined.

public class Documentation : System.Attribute
    string text;

    public Documentation(string text)
        this.text = text;

class SampleClass
    // This attribute will only be included if DEBUG is defined.
    [Documentation("This method displays an integer.")]
    static void DoWork(int i)

References :-

Programming is Easy…

Use of Obsolete attribute in C#

The Obsolete attribute marks a program entity as one that is no longer recommended for use. Each use of an entity marked obsolete will subsequently generate a warning or an error, depending on how the attribute is configured. For example:

[System.Obsolete("use class B")]
class A
    public void Method() { }
class B
    [System.Obsolete("use NewMethod", true)]
    public void OldMethod()  { }
    public void NewMethod()  { }

In this example the Obsolete attribute is applied to class A and to method B.OldMethod. Because the second argument of the attribute constructor applied to B.OldMethod is set to true, this method will cause a compiler error, whereas using class A will just produce a warning. Calling B.NewMethod, however, produces no warning or error.

The string provided as the first argument to attribute constructor will be displayed as part of the warning or error. For example, when you use it with the previous definitions, the following code generates two warnings and one error:

// Generates 2 warnings:
A a = new A();
// Generate no errors or warnings:
B b = new B();
// Generates an error, terminating compilation:

Two warnings for class A are generated: one for the declaration of the class reference, and one for the class constructor.

The Obsolete attribute can be used without arguments, but including an explanation of why the item is obsolete and what to use instead is recommended.

The Obsolete attribute is a single-use attribute and can be applied to any entity that allows attributes. Obsolete is an alias for ObsoleteAttribute.


References :-


Programming is Easy….

What is serialization?

Serialization is the process of converting an object into a stream of bytes. Deserialization is the opposite process of creating an object from a stream of bytes. Serialization / Deserialization is mostly used to transport objects (e.g. during remoting), or to persist objects (e.g. to a file or database).

Does the .NET Framework have in-built support for serialization?

There are two separate mechanisms provided by the .NET class library – XmlSerializer and SoapFormatter/BinaryFormatter. Microsoft uses XmlSerializer for Web Services, and uses SoapFormatter/BinaryFormatter for remoting. Both are available for use in your own code.

Can I customise the serialization process?

Yes. XmlSerializer supports a range of attributes that can be used to configure serialization for a particular class. For example, a field or property can be marked with the [XmlIgnore] attribute to exclude it from serialization. Another example is the [XmlElement] attribute, which can be used to specify the XML element name to be used for a particular property or field. Serialization via SoapFormatter/BinaryFormatter can also be controlled to some extent by attributes. For example, the [NonSerialized] attribute is the equivalent of XmlSerializer’s [XmlIgnore] attribute. Ultimate control of the serialization process can be acheived by implementing the the ISerializable interface on the class whose instances are to be serialized.

Why is XmlSerializer so slow?

There is a once-per-process-per-type overhead with XmlSerializer. So the first time you serialize or deserialize an object of a given type in an application, there is a significant delay. This normally doesn’t matter, but it may mean, for example, that XmlSerializer is a poor choice for loading configuration settings during startup of a GUI application.


Why do I get errors when I try to serialize a Hashtable?

XmlSerializer will refuse to serialize instances of any class that implements IDictionary, e.g. Hashtable. SoapFormatter and BinaryFormatter do not have this restriction.

What is reflection?

All .NET compilers produce metadata about the types defined in the modules they produce. This metadata is packaged along with the module (modules in turn are packaged together in assemblies), and can be accessed by a mechanism called reflection.

The System.Reflection namespace contains classes that can be used to interrogate the types for a module/assembly.
Using reflection to access .NET metadata is very similar to using ITypeLib/ITypeInfo to access type library data in COM, and it is used for similar purposes – e.g. determining data type sizes for marshaling data
across context/process/machine boundaries.

Reflection can also be used to dynamically invoke methods (see System.Type.InvokeMember ) , or even create types dynamically at runtime (see System.Reflection.Emit.TypeBuilder).

What is the difference between an event and a delegate?

An event is just a wrapper for a multicast delegate.
Adding a public event to a class is almost the same as adding a public multicast delegate field. In both cases, subscriber objects can register for notifications, and in both cases the publisher object can send notifications to the subscribers. However, a public multicast delegate has the undesirable property that external objects can invoke the delegate, something we’d normally want to restrict to the publisher.
Hence events – an event adds public methods to the containing class to add and remove receivers, but does not make the invocation mechanism public.

Create an EXE or Setup File in .NET Windows Application

explore .net

Hi .NET Architects!

Visual Studio Setup and deployment Projects provides to make an executable or installer file in .NET windows application is easy and rapidly.

Visual Studio provides templates for four types of deployment projects: Merge Module Project, Setup Project, Web Setup Project, and Cab Project. In addition, a Setup wizard is provided to help step you through the process of creating deployment projects. You can see the templates and the wizard in the New Project dialog box under the Setup and Deployment Projects node.

The following are guidelines for choosing the right type of deployment project for your project.

1)Merge Module Project : Packages components that might be shared by multiple applications.

2)Setup Project : Builds an installer for a Windows-based application.

3)Web Setup Project : Builds an installer for a Web application.

4)Cab Project: Creates a cabinet file for downloading to a legacy Web…

View original post 1,029 more words