开发者

Reflecting on executing assembly

开发者 https://www.devze.com 2023-02-10 10:31 出处:网络
I have the following linq statement builtIn = (from type in Assembly.GetExecutingAssembly().GetTypes()

I have the following linq statement

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
);

but it only works if I leave out the

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

Further down the code, I have to take this into account using

if (builtInInit.ToString开发者_开发百科() == "Pd3.Module Init()")

but I really don't like this solution so here's my 2 part question

  1. Is there a better way of getting methods where the namespace condition is correct? and
  2. Why do the type.Namespace fail?

With Regard, Stig


Types in the root namespace can have a null namespace, so it is calling .StartsWith on a null; just eliminate those first:

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

精彩评论

暂无评论...
验证码 换一张
取 消