class Method1
{
static int addi(params int[] values)
{
int sum=0;
foreach (int i in values)
sum+=i;
return sum;
}
static void Main()
{
Console.WriteLine(addi(3,4,7));
}
}
//爲什麼不能去掉PARAMS
using System;
class Method2
{
static void Arrays( int[] values)
{
foreach (int i in values)
Console.WriteLine(i);
}
static void Main()
{
int [] vaule1=new int []{100,200,300,400};
Arrays(vaule1);
}
}
//加與不加PARAMES都能通過編譯。
using System;
public class Method3
{
static void PrintArr(params int[] arr)
{
for (int i=0;i<arr.Length;i++)
arr[i]=i;
}
static void Main()
{
int [] arr={100,200,300};
PrintArr(arr);
foreach(int i in arr)
Console.Write(i+",");
}
}
//加與不加PARAMES都能通過編譯。
D:/Project/handing>Method2
100
200
300
400
D:/Project/handing>Method3
0,1,2,
D:/Project/handing>Method3
0,1,2,
D:/Project/handing>Method1
14
D:/Project/handing>
有點不能理解.
------------------------------------------------------------------------------------
params 有什麼用?
答:
params 關鍵字在方法成員的參數列表中使用,爲該方法提供了參數個數可變的能力
它在只能出現一次並且不能在其後再有參數定義,之前可以
示例:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class App
{
//第一個參數必須是整型,但後面的參數個數是可變的。
//而且由於定的是object數組,所有的數據類型都可以做爲參數傳入
public static void UseParams(int id, params object[] list)
{
Console.WriteLine(id);
for (int i = 0; i < list.Length; i++)
{
Console.WriteLine(list[i]);
}
}
static void Main()
{
//可變參數部分傳入了三個參數,都是字符串類型
UseParams(1, "a", "b", "c");
//可變參數部分傳入了四個參數,分別爲字符串、整數、浮點數和雙精度浮點數數組
UseParams(2, "d", 100, 33.33, new double[] { 1.1, 2.2 });
Console.ReadLine();
}
}
}
結果:
1
a
b
c
2
d
100
33.33
System.Double[]
-----------------------foreach實例
什麼是反射?
答:
反射,Reflection,通過它我們可以在運行時獲得各種信息,如程序集、模塊、類型、字段、屬性、方法和事件
通過對類型動態實例化後,還可以對其執行操作
一般用於插件式框架程序和設計模式的實現,當然反射是一種手段可以充分發揮其能量來完成你想做的任何事情(前面好象見過一位高人用反射調用一個官方類庫中未說明的函數。。。)
示例:
using System;
using System.Collections.Generic;
using System.Text;
namespace Example25Lib
{
public class Class1
{
private string name;
private int age;
//如果顯式的聲明瞭無參數構造函數,客戶端只需要用程序集的CreateInstance即可實例化該類
//在此特意不實現,以便在客戶調用端體現構造函數的反射實現
//public Class1()
//{
//}
public Class1(string Name, int Age)
{
name = Name;
age = Age;
}
public void ChangeName(string NewName)
{
name = NewName;
}
public void ChangeAge(int NewAge)
{
age = NewAge;
}
public override string ToString()
{
return string.Format("Name: {0}, Age: {1}", name, age);
}
}
}
反射實例化對象並調用其方法,屬性和事件的反射調用略去
using System;
using System.Collections.Generic;
using System.Text;
//注意添加該反射的命名空間
using System.Reflection;
namespace Example25
{
class Program
{
static void Main(string[] args)
{
//加載程序集
Assembly tmpAss = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + "Example25Lib.dll");
//遍歷程序集內所有的類型,並實例化
Type[] tmpTypes = tmpAss.GetTypes();
foreach (Type tmpType in tmpTypes)
{
//獲取第一個類型的構造函數信息
ConstructorInfo[] tmpConsInfos = tmpType.GetConstructors();
foreach (ConstructorInfo tmpConsInfo in tmpConsInfos)
{
//爲構造函數生成調用的參數集合
ParameterInfo[] tmpParamInfos = tmpConsInfo.GetParameters();
object[] tmpParams = new object[tmpParamInfos.Length];
for (int i = 0; i < tmpParamInfos.Length; i++)
{
tmpParams[i] = tmpAss.CreateInstance(tmpParamInfos[i].ParameterType.FullName);
if (tmpParamInfos[i].ParameterType.FullName == "System.String")
{
tmpParams[i] = "Clark";
}
}
//實例化對象
object tmpObj = tmpConsInfo.Invoke(tmpParams);
Console.WriteLine(tmpObj);
//獲取所有方法並執行
foreach (MethodInfo tmpMethod in tmpType.GetMethods())
{
//爲方法的調用創建參數集合
tmpParamInfos = tmpMethod.GetParameters();
tmpParams = new object[tmpParamInfos.Length];
for (int i = 0; i < tmpParamInfos.Length; i++)
{
tmpParams[i] = tmpAss.CreateInstance(tmpParamInfos[i].ParameterType.FullName);
if (tmpParamInfos[i].ParameterType.FullName == "System.String")
{
tmpParams[i] = "Clark Zheng";
}
if (tmpParamInfos[i].ParameterType.FullName == "System.Int32")
{
tmpParams[i] = 27;
}
}
tmpMethod.Invoke(tmpObj, tmpParams);
}
//調用完方法後再次打印對象,比較結果
Console.WriteLine(tmpObj);
}
}
Console.ReadLine();
}
}
}
結果:
Name: Clark, Age: 0
Name: Clark Zheng, Age: 27