Search This Blog

Monday, December 15, 2014

C# System.Net

C#System.Net

Provides  programming interface for:

  • network protocols
  • cache policies
  • e-mail
  • network traffic data and network address information
  • networking functionality
Classes:
Namespaces










STR SQL Server Example

SQL Server > Built-in Functions > STR

Converts number to varchar.


STR ( expression , length , decimal )

Example:

SELECT STR(123.45, 6, 1);

GO

result
 123.5





Assembly Class C# Example

C# > System > Reflection > Assembly

Assembly class:

  • load assemblies
  • explore the metadata 
  • find the types contained in assemblies and to create instances of those types.




Example


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication27
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Assembly a = typeof(Form1).Assembly;
            MessageBox.Show(a.FullName);
        }
    }
}





Reflection C#

C# > System > Reflection

Reflection provides objects that encapsulate assemblies, modules and types.
With reflection you can read metadata for finding assemblies, modules and types information at runtime. 
These objects also can be used to manipulate instances of loaded types.







Friday, December 5, 2014

Func C# Example

C# System Func

Func is a parameterized type. 

  • accepts any number and kinds of input parameters
  • one type of the return value T
  • stores anonymous methods 
Example

1. Func<T, TResult>


Func<int, int> power = p => p * p;
int[] numbers = { 1, 2, 3 };
IEnumerable<int> powers = numbers.Select(power);
foreach (int p in powers)
     Console.WriteLine(p);




Dynamic Language Runtime C#

C# System > Dynamic

Dynamic Language Runtime.






Dynamic Object Runtime C# Example

C# > Sytem.Dynamic > DynamicObject

DynamicObject specifies dynamic behavior at run time.






Example:

Create dynamic object in runtime.

Dynamically Add C# Properties at Runtime.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication26
{
    public class MyDynamicObject : DynamicObject
    {
        Dictionary<string, object> dictionary = new Dictionary<string, object>();
        public object this[string propertyName]
        {
            get
            {
                return dictionary[propertyName];
            }
            set
            {
                dictionary[propertyName] = value;
            }
        }
        public override bool TryGetMember(
            GetMemberBinder binder, out object result)
        {
            return dictionary.TryGetValue(binder.Name, out result);
        }
        public override bool TrySetMember(
            SetMemberBinder binder, object value)
        {
            dictionary[binder.Name] = value;
            return true;
        }
      
        public override bool TrySetIndex(
            SetIndexBinder binder, object[] indexes, object value)
        {
            int index = (int)indexes[0];
            if (dictionary.ContainsKey("Property" + index))
                dictionary["Property" + index] = value;
            else
                dictionary.Add("Property" + index, value);
            return true;
        }
    }
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dynamic obj = new MyDynamicObject();
            obj["column1"] = 1;
            obj.column2 = 2;

            var v1 = obj["column1"];
            var v2 = obj.column2;
        }
    }
}