C#反射示例 - 最佳实践 [英] C# reflection example - best practice

查看:68
本文介绍了C#反射示例 - 最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



不管是否有人可以通过反思获得最佳实践建议。

我得到的案例如下:

我有一个包含多个类的dll文件。其中一些类实现了特定的接口GetStream。这个接口有一个叫做Name的属性。

现在 - 我需要加载那个dll文件并调用一个实现GetStream的类的方法,其中属性Name =SomeName。

关于如何在不初始化所有类和检查实例的情况下如何做到这一点的任何建议?



我写了下面的代码,但它失败了一些为什么:< br $> b $ b

Hello,

Wonder if anyone can advice on best practice using a reflection.
The case I got is as follows:
I got a dll file with multiple classes. Some of those classes implement a particular interface "GetStream". This interface has a property called "Name".
Now - I need to load that dll file and invoke a method of a class implementing "GetStream", where property Name = "SomeName".
Any advice on how to do that without initializing all the classes and checking instances?

I have written the following code, but it fails somewhy:

// load the dll file
Assembly typeLibrary = Assembly.LoadFile(Directory.GetCurrentDirectory() + fileLoc);
string streamName = "SomeName";
foreach (Type t in typeLibrary.GetTypes())
{
   if (t.GetInterfaces().Contains(typeof(GetStream)) && t.Name.Equals(streamName))
   {
      // I never actually get here even, because t.GetInterfaces never contains "GetStream" for some reasons.
      // checking this with a debugger shows exact same type Name and FullName.. I must be missing something here...
   }
}





关于可能是什么的想法,我错过了什么?



顺便说一句 - 我宁愿避免将接口名称硬编码到像这样的字符串:



Any ideas of what that might be, that I am missing ?

By the way - I would prefer avoiding to hardcode the interface name into string like this:

t.GetInterface("GetStream") != null



事件hough它工作原理这看起来不像一个干净的方法,如果接口名称可能会导致运行时错误已更改(不会出现构建错误)。


Event hough it works this does not look like a clean approach and might cause runtime errors if the interface name is changed (there would be no build errors).

推荐答案

如果您删除&& t.Name.Equals(streamName) -part您的代码将进入if-block,前提是实际上有一个Type实现了该接口。



但这里存在一个概念问题:你只在这里处理类型。没有任何类型的实例,因此不能有任何属性值作为接口的一部分(因为那些不能是静态的)。



从理论上讲,您必须创建实现该接口的所有类型的实例,以便检查该属性的值。但它更像是你需要在这里重新思考你的想法。如果你详细说明为什么你认为你需要这样做,我们可能知道如何做到(不同,可能)。



编辑:顺便说一句: t.Name 不是Name-property的值。这是Type的名称。如果没有类型具有属性'Name'的巧合,则代码将无法编译。
If you remove the && t.Name.Equals(streamName) -part your code will enter the if-block provided that there actually is a Type that implements that interface.

But there's a conceptual problem here: You're dealing with Types only here. There are no instances of any Types and so there can't be any values of properties that are part of an interface (because those can't be static).

Theoretically you would have to create instances of all those Types which implement that interface in order to check the value of that property. But it smells more like you need to re-work your idea here. If you elaborate on why you think you need to do this, we might have an idea how it could be done (differently, probably).

Btw: t.Name isn't the value of the Name-property. It's the name of the Type. If there wasn't the coincidence that Type has a property 'Name' your code wouldn't compile.


这篇关于C#反射示例 - 最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆