C#通過腳本實(shí)現(xiàn)接口的示例詳解
以前C#腳本用的委托注入模式,今天在提示下,嘗試用腳本直接實(shí)現(xiàn)接口,然后C#可以動(dòng)態(tài)或指定新類型創(chuàng)建接口實(shí)現(xiàn)對(duì)象。從代碼角度看,稍顯復(fù)雜,但腳本方面顯得更簡(jiǎn)潔和有條理。
引用包需要Microsoft.CodeAnalysis、Microsoft.CodeAnalysis.Common等,其他自動(dòng)添加:
<?xml version="1.0" encoding="utf-8"?> <packages> <package id="Humanizer.Core" version="2.14.1" targetFramework="net472" /> <package id="Microsoft.Bcl.AsyncInterfaces" version="8.0.0" targetFramework="net472" /> <package id="Microsoft.CodeAnalysis" version="4.13.0" targetFramework="net472" /> <package id="Microsoft.CodeAnalysis.Analyzers" version="3.11.0" targetFramework="net472" developmentDependency="true" /> <package id="Microsoft.CodeAnalysis.Common" version="4.13.0" targetFramework="net472" /> <package id="Microsoft.CodeAnalysis.CSharp" version="4.13.0" targetFramework="net472" /> <package id="Microsoft.CodeAnalysis.CSharp.Scripting" version="4.13.0" targetFramework="net472" /> <package id="Microsoft.CodeAnalysis.CSharp.Workspaces" version="4.13.0" targetFramework="net472" /> <package id="Microsoft.CodeAnalysis.Scripting.Common" version="4.13.0" targetFramework="net472" /> <package id="Microsoft.CodeAnalysis.VisualBasic" version="4.13.0" targetFramework="net472" /> <package id="Microsoft.CodeAnalysis.VisualBasic.Workspaces" version="4.13.0" targetFramework="net472" /> <package id="Microsoft.CodeAnalysis.Workspaces.Common" version="4.13.0" targetFramework="net472" /> <package id="Microsoft.CSharp" version="4.7.0" targetFramework="net472" /> <package id="System.Buffers" version="4.5.1" targetFramework="net472" /> <package id="System.Collections.Immutable" version="8.0.0" targetFramework="net472" /> <package id="System.Composition" version="8.0.0" targetFramework="net472" /> <package id="System.Composition.AttributedModel" version="8.0.0" targetFramework="net472" /> <package id="System.Composition.Convention" version="8.0.0" targetFramework="net472" /> <package id="System.Composition.Hosting" version="8.0.0" targetFramework="net472" /> <package id="System.Composition.Runtime" version="8.0.0" targetFramework="net472" /> <package id="System.Composition.TypedParts" version="8.0.0" targetFramework="net472" /> <package id="System.IO.Pipelines" version="8.0.0" targetFramework="net472" /> <package id="System.Memory" version="4.5.5" targetFramework="net472" /> <package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net472" /> <package id="System.Reflection.Metadata" version="8.0.0" targetFramework="net472" /> <package id="System.Runtime.CompilerServices.Unsafe" version="6.0.0" targetFramework="net472" /> <package id="System.Text.Encoding.CodePages" version="7.0.0" targetFramework="net472" /> <package id="System.Threading.Channels" version="7.0.0" targetFramework="net472" /> <package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net472" /> </packages>
接口定義如下:
namespace WindowsFormsApp1 { public interface IFlexiblePluginAgent { string RegistDrType(); } }
驗(yàn)證文件TextFile1.txt如下:
using System; using System.Collections.Generic; public class FlexiblePluginAgentProcessScriptXXXX : WindowsFormsApp1.IFlexiblePluginAgent { public string RegistDrType() { return "scritp_drv"; } }
加載和驗(yàn)證代碼如下:
using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.Emit; using Microsoft.CodeAnalysis; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; namespace WindowsFormsApp1 { internal static class Program { /// <summary> /// 應(yīng)用程序的主入口點(diǎn)。 /// </summary> [STAThread] static void Main() { ScriptTest(); } static void ScriptTest() { try { // 讀取外部代碼文件 string codeFilePath = "TextFile1.txt"; string sourceCode = File.ReadAllText(codeFilePath); var compilation = CSharpCompilation.Create("DynamicAssembly") .WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)) .AddReferences(MetadataReference.CreateFromFile(typeof(object).Assembly.Location)) .AddReferences(MetadataReference.CreateFromFile(typeof(IFlexiblePluginAgent).Assembly.Location)) .AddSyntaxTrees(CSharpSyntaxTree.ParseText(sourceCode)); // 檢查編譯錯(cuò)誤 var diagnostics = compilation.GetDiagnostics(); if (diagnostics.HasAnyErrors()) { Console.WriteLine("編譯錯(cuò)誤:"); foreach (var diagnostic in diagnostics.Where(d => d.Severity == DiagnosticSeverity.Error)) { Console.WriteLine(diagnostic.ToString()); } return; } // 內(nèi)存中生成程序集 using (var ms = new MemoryStream()) { EmitResult emitResult = compilation.Emit(ms); if (!emitResult.Success) { Console.WriteLine("程序集生成失敗:"); foreach (var diagnostic in emitResult.Diagnostics.Where(d => d.Severity == DiagnosticSeverity.Error)) { Console.WriteLine(diagnostic.ToString()); } return; } ms.Seek(0, SeekOrigin.Begin); Assembly assembly = Assembly.Load(ms.ToArray()); Console.WriteLine("---檢查程序集中是否有IFlexiblePluginAgent的派生類---"); var derivedTypes = assembly.DefinedTypes .Where(t => typeof(IFlexiblePluginAgent).IsAssignableFrom(t) && !t.IsInterface) .ToList(); if (derivedTypes.Any()) { Console.WriteLine("找到以下IFlexiblePluginAgent的派生類:"); foreach (var type in derivedTypes) { Console.WriteLine($" - {type.FullName}"); } // 使用第一個(gè)派生類創(chuàng)建對(duì)象 Type agentType = derivedTypes.First().AsType(); IFlexiblePluginAgent agent = (IFlexiblePluginAgent)Activator.CreateInstance(agentType); string result = agent.RegistDrType(); Console.WriteLine($"注冊(cè)的 DrType 是: {result}"); } else { Console.WriteLine("未找到IFlexiblePluginAgent的派生類"); } Console.WriteLine("---知道類名字直接調(diào)用---"); Type agentType2 = assembly.GetType("FlexiblePluginAgentProcessScriptXXXX"); if (agentType2 == null) { Console.WriteLine("未找到 MyPluginAgent 類型"); return; } IFlexiblePluginAgent agent2 = (IFlexiblePluginAgent)Activator.CreateInstance(agentType2); string result2 = agent2.RegistDrType(); Console.WriteLine($"注冊(cè)的 DrType 是: {result2}"); } } catch (Exception ex) { Console.WriteLine($"發(fā)生錯(cuò)誤: {ex.Message}"); } } } // 擴(kuò)展方法用于檢查診斷信息 public static class DiagnosticExtensions { public static bool HasAnyErrors(this IEnumerable<Diagnostic> diagnostics) { return diagnostics.Any(d => d.Severity == DiagnosticSeverity.Error); } } }
到此這篇關(guān)于C#通過腳本實(shí)現(xiàn)接口的示例詳解的文章就介紹到這了,更多相關(guān)C#接口內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#創(chuàng)建Windows服務(wù)的圖文教程
本文主要介紹了C#創(chuàng)建Windows服務(wù)的圖文教程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06關(guān)于C# Math 處理奇進(jìn)偶不進(jìn)的實(shí)現(xiàn)代碼
下面小編就為大家?guī)硪黄P(guān)于C# Math 處理奇進(jìn)偶不進(jìn)的實(shí)現(xiàn)代碼。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-05-05C#分析URL參數(shù)并獲取參數(shù)和值對(duì)應(yīng)列表的方法
這篇文章主要介紹了C#分析URL參數(shù)獲取參數(shù)和值對(duì)應(yīng)列表的方法,涉及C#進(jìn)行URL分析及正則表達(dá)式的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03C#實(shí)現(xiàn)上位機(jī)的遠(yuǎn)程監(jiān)控與控制的詳細(xì)步驟
隨著工業(yè)自動(dòng)化、物聯(lián)網(wǎng)以及智能控制系統(tǒng)的普及,遠(yuǎn)程監(jiān)控與控制逐漸成為了許多系統(tǒng)的核心需求,上位機(jī)作為控制與監(jiān)測(cè)系統(tǒng)的核心,常用于接收處理來自下位機(jī)的數(shù)據(jù)并進(jìn)行控制操作,C#作為一門高效且功能強(qiáng)大的編程語言,本文將探討如何使用C#實(shí)現(xiàn)上位機(jī)遠(yuǎn)程監(jiān)控與控制2025-01-01C#將圖片存放到SQL SERVER數(shù)據(jù)庫(kù)中的方法
這篇文章主要介紹了C#將圖片存放到SQL SERVER數(shù)據(jù)庫(kù)中的方法,以實(shí)例形式較為詳細(xì)的分析了C#保存圖片到SQL Server數(shù)據(jù)庫(kù)的具體步驟與相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09C#中Forms.Timer、Timers.Timer、Threading.Timer的用法分析
這篇文章主要介紹了C#中Forms.Timer、Timers.Timer、Threading.Timer的用法分析,以實(shí)例形式較為詳細(xì)的講述了.NET Framework里面提供的三種Timer具體用法,需要的朋友可以參考下2014-10-10