C# 反射加載接口


1 . 新建接口類 Imanager

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InterfaceLoading
{
public interface Imanager
{
/// <summary>
/// 描述
/// </summary>
string Summary { get; set; }
/// <summary>
/// 加載類型,如Oracle,MySql,API等
/// </summary>
string LoadType { get; set; }

/// <summary>
/// 版本號
/// </summary>
int Ver { get;}

/// <summary>
///
/// </summary>
void Loadding();

}
}

2 . 新建接口實現子類1 Manager1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InterfaceLoading
{
public class Manager1 :Imanager
{
public static readonly Manager1 Instance = new Manager1();
public string Summary
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}

public string LoadType
{
get
{
return "Manager1";
}
set
{
value = LoadType;
}
}

public int Ver
{
get { throw new NotImplementedException(); }
}

public void Loadding()
{

}
}
}

 

3 . 新建接口實現子類2 Manager2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InterfaceLoading
{
public class Manager2:Imanager
{
public string Summary
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}

public string LoadType
{
get
{
return "Manager2";
}
set
{
value = LoadType;
}
}

public int Ver
{
get { throw new NotImplementedException(); }
}

public void Loadding()
{
throw new NotImplementedException();
}
}
}

4 . 創建接口加載提供類 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InterfaceLoading
{
public class LoadProvider
{
/// <summary>
/// 配置文件加載接口
/// </summary>
public Imanager ConfigLoader { get; protected set; }

/// <summary>
/// 所有的配置文件加載類loader
/// </summary>
private Dictionary<string, Imanager> loaders = new Dictionary<string, Imanager>();
/// <summary>
/// 所有的配置文件加載類loader
/// </summary>
public Dictionary<string, Imanager> Loaders
{
get { return loaders; }
}
public void Load()
{
// 反射加載接口Imanager的實現集合
var types = GetType().Assembly.GetTypes().Where(p=>p.GetInterface(typeof(Imanager).FullName)!=null);
foreach (var ctype in types)
{
Imanager cload = Activator.CreateInstance(ctype) as Imanager;
if (cload != null)
{
if (!Loaders.ContainsKey(cload.LoadType))
{
Loaders.Add(cload.LoadType, cload);
}
}
}
//設置默認的配置讀取信息
this.SetDefaultLoader(System.Configuration.ConfigurationManager.AppSettings["ConfigType"]);
}

/// <summary>
/// 設置默認的加載接口
/// </summary>
/// <param name="loaderType">加載類型 Oacle,File等</param>
public void SetDefaultLoader(string loaderType)
{
if (!string.IsNullOrEmpty(loaderType))
{
foreach (var loader in loaders)
{
if (loader.Key.ToUpper() == loaderType.ToUpper())
{
ConfigLoader = loader.Value;
break;
}
}
if (ConfigLoader == null)
{
//LogProvider.Create().WriteLog(inCom.Logs.ErrorCodes.StaticErrorCodes.x000001, "不支持當前的配置文件讀取方式,當前要求的讀取方式是:" + loaderType, AppDomain.CurrentDomain.BaseDirectory, "");
}

}
else
{
// LogProvider.Create().WriteLog(inCom.Logs.ErrorCodes.StaticErrorCodes.x000001, "不支持當前的配置文件讀取方式,當前要求的讀取方式是:" + loaderType, "", "");
}
}

}
}

5 . 啟動加載

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InterfaceLoading
{
class Program
{
static void Main(string[] args)
{
LoadProvider lp = new LoadProvider();
lp.Load();
Console.Read();
}
}
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM