.net3.5新しい機能
23073 ワード
このキーワードでクラスに拡張メソッドを追加する
インタフェースを拡張する方法でもあり、インタフェースを実装するクラスにもその方法があります.
反射:
.NET反射の定義:メタデータをレビューし、そのタイプ情報を収集する機能.
http://www.cnblogs.com/focusonnet/archive/2009/04/17/1438013.htmlこの文章はいいですね.
プログラムセット:
コンソールコード:
特性:また明日見ましょう
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace test
{
class Class1
{
public string code = "2";
private void Show()
{
Console.Write(this.GetAddress());
}
}
public static class Class2
{
public static string GetAddress(this Class1 c1)
{
switch (c1.code)
{
case "1":
return " ";
case "2":
return " ";
default:
return " ";
}
}
}
}
インタフェースを拡張する方法でもあり、インタフェースを実装するクラスにもその方法があります.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace test
{
interface Inter
{
public string Code { get; set; }
}
public class Class1 : Inter
{
private void Show()
{
Console.Write(this.GetAddress());
}
private void Haha()
{
Console.Write(" ".GetHaha());
}
public string Code
{
get;
set;
}
}
public static class Class2
{
public static string GetAddress(this Inter c1)
{
switch (c1.Code)
{
case "1":
return " ";
case "2":
return " ";
default:
return " ";
}
}
public static string GetHaha(this string str)
{
return str + " ";
}
}
}
反射:
.NET反射の定義:メタデータをレビューし、そのタイプ情報を収集する機能.
http://www.cnblogs.com/focusonnet/archive/2009/04/17/1438013.htmlこの文章はいいですね.
プログラムセット:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ClassLibrary1
{
public interface IWel
{
String Print();
}
public class Arithmetic:IWel
{
int _num1;
public int Num1 { get { return _num1; } set { _num1 = value; } }
int _num2;
public int Num2 { get { return _num2; } set { _num2 = value; } }
public Arithmetic() { }
public Arithmetic(int num1, int num2)
{
_num1 = num1;
_num2 = num2;
}
public String Add(int num1, int num2)
{
Console.WriteLine("{0}+{1}={2}", num1, num2, num1 + num2);
return "Add(int num1,int num2) ";
}
private string Add()
{
return "Add() ";
}
private void Subtration(int num1, int num2)
{
Console.WriteLine("{0}-{1}+Subtration(int num1,int num2)" +
" ", num1, num2, num1 - num2);
}
public static void Multiplication(int num1, int num2)
{
Console.WriteLine("{0}*{1}={2} Multipcation(int num1,int num2)" +
" ", num1, num2, num1 * num2);
}
private static void Multiplication()
{
Console.WriteLine("Multiplication() ";
}
#region Iwel
public string Print()
{
return " ";
}
#endregion
}
}
コンソールコード:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary1;
using System.Reflection;
namespace ConsoleApplication1
{
class Program
{
delegate void TestDelegage(int num1, int num2);
static void Main(string[] args)
{
Assembly assembly = Assembly.Load("ClassLibrary1");
Console.WriteLine(" ");
foreach (Type var in assembly.GetTypes())
{
Console.WriteLine(var.Name + " ClassLibrary1 ");
}
Console.WriteLine(" ");
Module[] modules = assembly.GetModules();
foreach (Module va in modules)
{
Console.WriteLine(va.Name);
}
Type type = typeof(ClassLibrary1.Arithmetic);
Console.WriteLine("{0} public {1}", type, type.IsPublic);
PropertyInfo[] memberinfo = type.GetProperties();//
foreach (PropertyInfo var in memberinfo)
{
Console.WriteLine(type + " " + var.Name);
}
Type[] t=type.GetInterfaces();
foreach(Type var in t)
{
Console.WriteLine(var.Name+" ");
}
MethodInfo[] method = type.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic);
foreach (MethodInfo var in method)
{
Console.WriteLine(" : ={0} ={1}", var.Name, var);
}
MethodInfo[] methodpublic = type.GetMethods(BindingFlags.Instance | BindingFlags.Public);
foreach (MethodInfo var in methodpublic)
{
Console.WriteLine(" : ={0} {1}", var.Name, var);
}
MethodInfo[] methodstatic = type.GetMethods(BindingFlags.Public | BindingFlags.Static);
foreach (MethodInfo var in methodstatic)
{
Console.WriteLine(" ={0} ={1}", var.Name, var);
}
MethodInfo[] methodprivatestatic=type.GetMethods(BindingFlags.NonPublic|BindingFlags.Static)
foreach(MethodInfo var in methodprivatestatic)
{
Console.WriteLine(" ={0} {1}",var.Name,var);
}
ConstructorInfo[] con=type.GetConstructors();
foreach(ConstructorInfo var in con)
{
Console.WriteLine(var);
}
object obj=Activator.CreateInstance(type,null);
MethodInfo men1=type.GetMethod("Add");
Console.WriteLine(" {0} ",men1);
object[] nums1={5,4};
Console.WriteLine(men1.Invoke(obj,nums1));
MethodInfo men2=type.GetMethod("Add",BindingFlags.Instance|BindingFlags.NonPublic);
Console.WriteLine(men2.Invoke(obj,nums1));
MethodInfo men3=type.GetMethod("Multiplication",BindingFlags.Public|BindingFlags.Static);
object[] nums2={5,6};
men3.Invoke(null,nums2);
MethodInfo men4=type.GetMethod("Multiplication",BindingFlags.Static|BindingFlags.NonPublic);
men4.Invoke(null,null);
TestDelegage dele=(TestDelegage)Delegate.CreateDelegate(typeof(TestDelegage),obj,"Subtration");
dele(9,3);
}
}
}
特性:また明日見ましょう