Menu Close

Blog

Slide 1

Microsoft Business Applications Blogposts, YouTube Videos and Podcasts

Helping Businesses with Technology

Slide 2

Microsoft Business Applications Blogposts, YouTube Videos and Podcasts

Helping Businesses with Technology

Slide 3

Microsoft Business Applications Blogposts, YouTube Videos and Podcasts

Helping Businesses with Technology

previous arrow
next arrow

REFLECTIONS IN C#

REFLECTIONS IN C#


“is the ability of inspecting assemblies’ metadata at runtime. it is used to find all types in an  assembly and /or dynamically invoke methods in an assembly.”


Uses of reflection:

1. When you drag and drop a button on a win forms or asp.net application. The properties window uses reflection to show all the properties of the Button class. So, reflection is extensivley used by IDE or a UI designers.


2. Late binding can be achieved by using reflection. You can use reflection to dynamically create an instance of a type, about which we don’t have any information at compile time. So, reflection enables you to use code that is not available at compile time.

3. Consider an example where we have two alternate implementations of an interface. You want to allow the user to pick one or the other using a config file. With reflection, you can simply read the name of the class whose implementation you want to use from the config file, and instantiate an instance of that class. This is another example for late binding using reflection.

————————————————————————————————————————–
using System;
using System.Reflection;

namespace ConsoleApplication4
{
    public class MainClass
    {
       
        private static void Main()
        {    
            Type T = Type.GetType(“ConsoleApplication4.Customer”);
            Console.WriteLine(“Full Name = {0}”, T.FullName);
            Console.ReadLine();
            Console.WriteLine(“Just the Name = {0}”, T.Name);
            Console.WriteLine(“Just the Namespace = {0}”, T.Namespace);

            Console.ReadLine();

            Console.WriteLine(“Properties in Customer”);
            PropertyInfo[] properties = T.GetProperties();
            foreach (PropertyInfo property in properties)
            {
                Console.WriteLine(property.PropertyType.Name + ” ” + property.Name);
                Console.ReadLine();
            }

            Console.ReadLine();

            Console.WriteLine(“methods in Customer class”);
            MethodInfo[] methods = T.GetMethods();
            foreach (MemberInfo method in methods)
            {
                Console.WriteLine(method.ReflectedType.Name + ” ” + method.Name);
                Console.ReadLine();
            }


            Console.ReadLine();

            Console.WriteLine(“Constructor in Customer class”);
            ConstructorInfo[] constructors = T.GetConstructors();
            foreach (ConstructorInfo constructor in constructors)
            {
                Console.WriteLine(constructor.Name);
                Console.ReadLine();
            }
        }
    }
}
    public class Customer
    {
        public int Id { get; set; }
        public string Name { get; set; }

        public Customer(int ID, string Name)
        {
            this.Id = ID;
            this.Name = Name;
        }
        public Customer()
        {
            this.Id = -1;
            this.Name = string.Empty;

        }
        public void PrintID()
        {
            Console.WriteLine(“ID= {0}”, Id);
            Console.ReadLine();
        }
        public void PrintName()
        {
            Console.WriteLine(“Name ={0}”, Name);
            Console.ReadLine();
        }
    }


==============================================























Share this:

ATTRIBUTES IN C#

Attributes

Attributes allow you to add declarative information to your programs. This information can then be queried at runtime using reflection

There are several Pre-defined attributes provided by .NET.It is also possible to create your own Custom Attributes.

A few pre-defined attributes with in the .NET framework.
obsolete    – Marks types and type members outdated
WebMethod  – To expose a method as an XML Web service method
Serializable  – Indicates that a class can be serialized


It is possible to customize the attributes using parameters
An attributes is a class that inherits from SYSTEM.ATTRIBUTES base class.
————————————————————————————————————————–

using System;
using System.Collections.Generic;

namespace ConsoleApplication4
{
    public class MainClass
    {
        private static void Main()
        {
            Calculator.AddNumber(new List<int>() {10,20, 30});
           

        }
    }

    public class Calculator
    {
        [Obsolete(“Use(List<int> Numbers)Method”,true)]
        public static int AddNumber(int FirstNumber, int SecondNumber)
        {
            return FirstNumber + SecondNumber;
        }

        public static int AddNumber(List<int> Numbers)
        {
            int Sum = 0;
            foreach (int Number in Numbers)
            {
                Sum = Sum + Number;
            }
            return Sum;
        }
    }

}

Share this:

ACCESS MODIFIERS FOR TYPES(CLASSES) IN C#

CLASSES, STRUCTURES, INTERFACES, DELEGATES WILL HAVE TYPES AND CAN ONLY USE INTERNAL OR PUBLIC ACCESS MODIFIERS

using System;
namespace ConsoleApplication4
{
    public class AssemblyOne
    {
        public void PrinID()
        {
            Console.WriteLine(“print this is a types example”);
        }
    }
}
// default  is internal for type classes like classes, structures, interfaces, delegates, etc..
// default  is private for type members like fields, properties, methods, etc 
using System;
using AssemblyOne;
namespace ConsoleApplication4
{
internal class AssemblyTwo 
    {
        AssemblyOne instance = new AssemblyOne();
        instance.xxxxxx     = xyz;
    }
}
Share this:

ACCESS MODIFIERS IN C#

ACCESS MODIFIERS IN C#

Types Vs Type Members


In C# there are 5 different access modifiers in c#
1.Private
2.Protected
3.Internal
4. Protected Internal
5. Public

Private members are available only with in the containing type, where as public members are available any where. There is no restricction.

Protected Members are available, with in the containing type and to the types that derive from the containing type

Access Modifier                       Accessibility
Private ->                  Only with in the containing class
Public ->                  Any where, No Restrictions
Protected ->                 With in the containing types and the types derived from the
                                                 containing type.
————————————————————————————————————————-

using System;
using System.IO;

namespace ConsoleApplication4
{

    //public class Customer
    //{

    //    private int _id;

    //    public int Id
    //    {
    //        get { return _id; }
    //        set { _id = value; }

    //    }
    //}

    public class Customer
    {
        protected int _id;
    }

    public class corporateCustomer : Customer
    {
        public void printID()
        {
            corporateCustomer cc = new corporateCustomer();
            cc._id = 101;
        }
    }

   public  class Program
    {
        public static void Main()
    {
        Customer C1 = new Customer();
        Console.WriteLine(C1._id);
    }

    }
}
————————————————————————————————————————-

INTERNAL AND PROTECTED INTERNAL ACCESS MODIFIERS IN C#


INTERNAL AND PROTECTED INTERNAL
A member with internal access modifier is available anywhere with in the containing assembly. it’s a compile time error to access, an internal member from outside the containing assembly.
Protected Internal members can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly. It is a combination of protected and internal. If you have understood protected and internal, this should be very easy to follow. 



Access Modifier                       Accessibility
Private                    Only with in the containing class
Public                   Any where, No Restrictions
Protected                   With in the containing types and the types derived from the 
                                                 containing type.
Internal                  Anywhere with in the containing assembly
ProtectedInternal         Anywhere with in the containing assembly, and from within a 

                                                 derived class in any another assembly.



INTERNAL:- 

Internal fields are accessible within the assembly where it is defined not outside of the assembly.
PROTECTED INTERNAL:-
Protected Internal are accessible within the assembly and in the derived class through inheritance within another assembly(refering the base class in the derived class through reference or namespace declaration).

Share this:

UNDERSTANDING DIFFERENCE BETWEEN TYPES AND TYPE MEMBERS IN C#

DIFFERENCE BETWEEN TYPES AND TYPE MEMBERS IN C#

Types Vs Type Members

In this example Customer is the Type and fields, properties and method are type members.

So, in general classes, structs, enums, interfaces, delegates are called as types and fields, properties, constructors, methods etc.. that normally reside in a type are called  as type members.

In C# there are 5 different access modifiers:
1.Private
2.Protected
3.Internal
4. Protected Internal
5. Public

Type members can have all the access modifiers, where as types can have only 2 (internal , public) of the 5 access modifiers

Note: Using regions you can expand and collapse sections of your code either manually, or using visual studio Edit>Outling > Toggle All Outlining

using System;
using System.IO;

namespace ConsoleApplication4
{
    class Program
    {
        public static void Main()
        {

        }

    }

    public class Customer
    { 
        #region fields
        private int _id;
        private string _firstName;
        private string _lastName;
        #endregion

        #region properties
        public int Id
        {
            get { return _id; }
            set { _id = value; }

        }
        public string FirstName
        {
            get { return _firstName; }
            set { _firstName = value; }

        }
        public string LastName
        {
            get { return _lastName;}
            set { _lastName = value; }
        }
        #endregion

        #region methods
        public string GetFullName()
        {
            return this.FirstName + ” ” + this.LastName;
        }
        #endregion


    }
}

Share this: