Sunday 21 August 2016

Static and Abstract Confusion

Non static class can have static and non-static Members.

Static class can not have instance members.


Non-Abstract class can not have abstract members.

Abstract class have abstract and non-abstract members.



Wednesday 17 February 2016

2 calls in WCF

When you get 2 calls from WCF, usually means serialization problems

Tuesday 8 December 2015

Read Service End points , Method Details in client Side using Reflection

 [HttpPost]
        [AllowAnonymous]
        public JsonResult ListServiceEndPoints()
        {
            ClientSection clientSection = (ClientSection)ConfigurationManager.GetSection("system.serviceModel/client");

            List<WcfServiceEndPointDetails> endPointDetails = new List<WcfServiceEndPointDetails>();

            for(int i = 0; i < clientSection.Endpoints.Count; i++)
            {
                var endPoint = clientSection.Endpoints[i];
                List<EndPointContractOperations> operationDetails = GetContractOperationDetails(endPoint);
                endPointDetails.Add(new WcfServiceEndPointDetails()
                {
                    EndPointName = endPoint.Name,
                    EndPointAddress = endPoint.Address.ToString(),
                    ContractName = endPoint.Contract,
                    ContractOperations = operationDetails
                });
            }
            return Json(new { Success = true, Data = endPointDetails });
        }

        /// <summary>
        /// Get Operations Details
        /// </summary>
        /// <param name="endPoint">endPoint</param>
        /// <returns>EndPoint Contract Operations</returns>
        private List<EndPointContractOperations> GetContractOperationDetails(ChannelEndpointElement endPoint)
        {
            List<EndPointContractOperations> endPointContractOperations = new EditableList<EndPointContractOperations>();

            try
            {
                ContractDescription contractDescription =
                    ContractDescription.GetContract(
                        typeof (IAuthenticationSessionApi));
                foreach (OperationDescription operation in contractDescription.Operations)
                {
                    endPointContractOperations.Add(new EndPointContractOperations()
                    {
                        OperationName = operation.Name,
                        Signature = MethodSignature(operation.SyncMethod),
                        OperationDescription = string.Empty
                    });
                }
            }
                //// Empty Catch -  If any Contract throw error then it will continue to read other Contract Operations
            catch (Exception)
            {
            }

            return endPointContractOperations;
        }

        public static string MethodSignature(MethodInfo mi)
        {
            String[] param = mi.GetParameters()
                .Select(p => String.Format("{0} {1}", p.ParameterType.Name, p.Name))
                .ToArray();


            string signature = String.Format("{0} {1}({2})", mi.ReturnType.Name, mi.Name, String.Join(",", param));

            return signature;
        }

Monday 20 April 2015

Reflection Examples [C#]

This example shows how to dynamically load assembly, how to create object instance, how to invoke method or how to get and set property value.

Create instance from assembly that is in your project References

The following examples create instances of DateTime class from the System assembly.
[C#]
// create instance of class DateTime
DateTime dateTime = (DateTime)Activator.CreateInstance(typeof(DateTime));

[C#]
// create instance of DateTime, use constructor with parameters (year, month, day)
DateTime dateTime = (DateTime)Activator.CreateInstance(typeof(DateTime),
                                                       new object[] { 2008, 7, 4 });

Create instance from dynamically loaded assembly

All the following examples try to access to sample class Calculator from Test.dll assembly. The calculator class can be defined like this.
[C#]
namespace Test
{
    public class Calculator
    {
        public Calculator() { ... }
        private double _number;
        public double Number { get { ... } set { ... } }
        public void Clear() { ... }
        private void DoClear() { ... }
        public double Add(double number) { ... }
        public static double Pi { ... }
        public static double GetPi() { ... }
    }
}

Examples of using reflection to load the Test.dll assembly, to create instance of the Calculator class and to access its members (public/private, instance/static).
[C#]
// dynamically load assembly from file Test.dll
Assembly testAssembly = Assembly.LoadFile(@"c:\Test.dll");

[C#]
// get type of class Calculator from just loaded assembly
Type calcType = testAssembly.GetType("Test.Calculator");

[C#]
// create instance of class Calculator
object calcInstance = Activator.CreateInstance(calcType);

[C#]
// get info about property: public double Number
PropertyInfo numberPropertyInfo = calcType.GetProperty("Number");

[C#]
// get value of property: public double Number
double value = (double)numberPropertyInfo.GetValue(calcInstance, null);

[C#]
// set value of property: public double Number
numberPropertyInfo.SetValue(calcInstance, 10.0, null);

[C#]
// get info about static property: public static double Pi
PropertyInfo piPropertyInfo = calcType.GetProperty("Pi");

[C#]
// get value of static property: public static double Pi
double piValue = (double)piPropertyInfo.GetValue(null, null);

[C#]
// invoke public instance method: public void Clear()
calcType.InvokeMember("Clear",
    BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public,
    null, calcInstance, null);

[C#]
// invoke private instance method: private void DoClear()
calcType.InvokeMember("DoClear",
    BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.NonPublic,
    null, calcInstance, null);

[C#]
// invoke public instance method: public double Add(double number)
double value = (double)calcType.InvokeMember("Add",
    BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public,
    null, calcInstance, new object[] { 20.0 });

[C#]
// invoke public static method: public static double GetPi()
double piValue = (double)calcType.InvokeMember("GetPi",
    BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public,
    null, null, null);

[C#]
// get value of private field: private double _number
double value = (double)calcType.InvokeMember("_number",
    BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic,
    null, calcInstance, null);

Sunday 19 April 2015

select n th row of table using jquery

  1. $(document).ready(function(){  
  2.   
  3.    //select the second row of table and change font color to red  
  4.    $('table tbody tr:nth-child(2)').css('color','red');  
  5.   
  6. });  

Sunday 12 April 2015

Unmanaged Resource



Unmanaged -  The most common types of unmanaged resource are objects that wrap operating system resources, such as files, windows, network connections, or database connections. 

Saturday 21 March 2015

Objects and Types in Javascript

4 types of Object
Intrinsic objects   - Date ,String .number,.....
Host Objects - Window ,document
Creating Objects -  var obj =new Object();
Activex Object

The type of all objects created with a custom constructor is object. There are only six types in JavaScript:objectfunctionstringnumberboolean, and undefined.