我有以下linq声明

builtIn = (from type in Assembly.GetExecutingAssembly().GetTypes()
where type.IsClass && type.Namespace.ToUpper().StartsWith("PD3.MODULES.BUILTIN.")
let onStartMethod = type.GetMethod("Init")
where onStartMethod != null
select onStartMethod
);


但只有在我遗漏了

&& type.Namespace.ToUpper().StartsWith("PD3.MODULES.BUILTIN")部分:(

在代码的后面,我必须考虑使用

if (builtInInit.ToString() == "Pd3.Module Init()")


但我真的不喜欢这种解决方案,所以这是我的两部分问题


有没有更好的方法来获取名称空间条件正确的方法?

为什么type.Namespace失败?


就,关于,
斯蒂格

最佳答案

根名称空间中的类型可以具有一个空名称空间,因此它在一个空名称上调用.StartsWith。只需先消除这些:

... && type.Namespace != null && type.Namespace. {blah}

关于c# - 反射(reflection)执行程序集,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4969179/

10-14 20:50
查看更多