- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以这是交易。我有我的解决方案,其中有几个项目:
我的包装器应用程序包含我的 app.config,我的插件应该直接引用它来进行 self 配置。这样,我的包装器应用程序不需要知道任何其他信息,只需调用适配器工厂来更新插件实例。
<configuration>
<appSettings>
<add key="Plugin" value="Prototypes.BensPlugin" />
<add key="Prototypes.BensPlugin.ServiceAddress" value="http://localhost/WebServices/Plugin.asmx" />
<add key="Prototypes.BensPlugin.Username" value="TestUserName" />
<add key="Prototypes.BensPlugin.Password" value="TestPassword" />
</appSettings>
</configuration>
包装项目:
using Worker;
public class Program
{
public static void Main()
{
var serviceProc = new ServiceProcess();
serviceProc.DoYourStuff();
}
}
worker 项目:
using PluginLibrary;
namespace Worker
{
public class ServiceProcess
{
public string GetRequiredAppSetting(string settingName)
{
/* Code to get a required configuration setting */
}
public void DoYourStuff()
{
string pluginTypeName = GetRequiredAppSetting("Plugin");
//At this point, pluginTypeName = "Prototypes.BensPlugin"
Type type = Type.GetType(pluginTypeName); //type == null, wth!?
//string testTypeName = new BensPlugin().GetType().ToString();
///At this point, testTypeName = "Prototypes.BensPlugin"
//Type testType = Type.GetType(testTypeName)
///And testType still equals null!
if (type != null)
{
IPlugin plugin = PluginFactory.CreatePlugin(type);
if (plugin != null)
plugin.DoWork();
}
}
}
}
插件库:
namespace PluginLibrary
{
public interface IPlugin
{
void DoWork();
}
public class PluginFactory
{
public IPlugin CreatePlugin(Type pluginType)
{
return (IPlugin)Activator.CreateInstance(pluginType);
}
}
}
插件:
using PluginLibrary;
namespace Prototypes
{
public class BensPlugin : IPlugin
{
public string ServiceAddress { get; protected set; }
public string username { get; protected set; }
public string password { get; protected set; }
public void DoWork()
{
Trace.WriteLine("Ben's plugin is working.");
}
public BensPlugin()
{
/* Code to self configure from app.config */
}
}
}
好的,到此为止。当我引用 Type.GetType(pluginTypeName)
时,我的工作项目出现了问题。我的 pluginTypeName
已从配置中正确提取,但 Type.GetType(pluginTypeName)
返回 null
。我尝试直接在代码中的同一位置更新我的插件实例:
var obj = new BensPlugin();
这工作得很好,obj.GetType().ToString()
返回与我在 app.config
中配置的完全相同的字符串。
谁能告诉我为什么我可以在代码中的那个点新建一个具体对象,但是 Type.GetType(pluginTypeName)
会失败?
最佳答案
可能是您需要指定该类型位于哪个程序集中:
<add key="Plugin" value="Prototypes.BensPlugin, TheAssemblyName" />
关于c# - 为什么我不能通过 Type.GetType() 找到我在 app.config 中引用的插件实例的类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1688420/
我已经阅读了“UnderlyingSystemType”的定义,即它“表示公共(public)语言运行时提供的表示此类型的类型”。 SO 上有一个相关链接 When does the Underlyi
以下代码会产生不同的结果: class X { public class Y { } } ... var t = typeof(X.Y); var n = t.ToString().Dump(
这个问题在这里已经有了答案: How costly is .NET reflection? (13 个答案) 关闭 9 年前。 我在 .NET/C# 中使用反射 API,例如 GetType() 和
private int buType=0; public int getType() { return buType; } 上述代码片段是在扩展 JDialog 类的类中编写的。它在 Java
我试图在运行时从程序集列表中定位一个类型;我的代码是这样的: foreach (Assembly assembly in assembliesToSearch) { Type t = asse
System.Object.GetType() 和 System.Type.GetType() 有区别吗 最佳答案 System.Type 派生自 System.Object。因此,它从 System
当我在 Powershell(已安装 v3)中观察到一些奇怪的行为时,我正在整理一个简单的演示脚本。 $file = ls 'C:\temp' | Where-Object {$_.Extension
我正在使用 Rome 和 SyndEntry(com.sun.syndicate.feed.synd.SyndEntry) 来检索 URL 的“getType”字段。 这是示例 xml feed。
我有一个 C# 程序集,但我没有它的依赖项程序集。 有没有办法在没有其依赖项程序集的情况下获取 .Net 程序集的 GetTypes()。 Assembly SampleAssembly; Sam
在 foreach 语句的帮助下,我在我的 WinForms 表单中的一些 controls 中搜索。我正在比较通过“is”-reference(a is DataGridView)找到的对象。 “a
我正在制作 XNA 游戏,每次更新我调用以下代码 2 到 20 次。我尝试使用谷歌搜索,这似乎有点慢,所以我只是想问问是否有更快的方法来比较类型? 代码: public Modifier th
我在 NewStarterTool 命名空间中有以下 Globals 类: static class Globals { static internal class FieldNames
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: .NET: Determine the type of “this” class in its static
为什么会这样: Object o = "my string"; Console.WriteLine(o.GetType()); 输出: System.String 如果函数调用被分派(dispatch
因此,我对自定义类型方面的数据如何存储在 .NET Framework 中没有深入的了解,但我一直在寻找关于转换系统如何工作的解释。 例如,如果要从像 Char 这样的 ValueType 结构显式转
我遇到了这段代码: int myInt = 0; textBox1.Text = myInt.GetType().Name; 根据.NET documentation , GetType() 是一个方
在处理嵌套类并将类型名称输出到控制台窗口时,我注意到了一些奇怪的事情。我想知道是否有人可以为我解释一下。在主类上调用 GetType() 时,它会返回我所期望的,即相关命名空间之后的类名。即 Name
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: .NET: Determine the type of “this” class in its static
我有一个可能不包含任何项目的初始化数组。 我们称它为a, 在a 上调用GetType() 显然会返回一种数组类型。是否可以获取数组包含的项的类型? 显然 a[0].GetType() 可以工作,但数组
基于几天前在 SO 中提出的以下问题:GetType() and polymorphism和阅读 Eric Lippert's回答,我开始考虑如果让 GetType() 不是虚拟的真的确保一个对象不能
我是一名优秀的程序员,十分优秀!