1.設計接口類:
public interface IBase
{
void Somefun();
}
2.設計擴展插件類:
class PluginCls:IBase
{
public void Somefun()
{
Console.WriteLine("hello ,i am derived class");
}
}
3.主程序設計:
class Program
{
static void Main(string[] args)
{
Assembly asm = Assembly.LoadFile(@"....\PluginCls.dll");
Type[] clsTypes = asm.GetTypes();//可訪問程序集所有定義的類型
//Type[] clsTypes = asm.GetExportedTypes();//這個方法只能訪問程序集公開定義的類型
foreach (Type e in clsTypes)
{
if (e.IsClass && typeof(IBase).IsAssignableFrom(e))//兼容匹配
{
IBase mth = (IBase)Activator.CreateInstance(e);//類實例構造
mth.Somefun();//調用插件類的實現方法
}
}
Console.ReadKey();
}
}
程序輸出:

這樣主應用程序可以加載由其他人員設計的插件,無需關心該插件所定義的新類型,只要插件按照給定的接口原則設計,
主程序則可無縫加載。
參考:《CLR via C#》第22章,第23章
jeffrey大牛實現太屌了,寫的書本本經典,牆裂推薦他的書!
#define n 20
main () {
int a[n] = {.....};/* 數據你事先定義也好,錄入也好*/
int i, max;
max=0;
for (i=0;i<n;i++) {
if (a[i]>max) max=a[i];
}
printf("The max number is %d\n", a[i]);
}
第一題沒明白題意
第二題:
#include "stdio.h"
void main()
{
int t,n;
printf("Please input a number:\n");
scanf("%d",&n);
while(n)
{
t=n%10;//求余,得出最後一位
printf("%d",t);
n=n/10;//除10,去掉最後一位
}
}
第三題:
#include "stdio.h"
void main()
{
int i;
for(i=1;i<=100;i++)
{
if(i%5==0)
continue;
printf("%d\t",i);
}
}