我目前正在提取有关从这里型方法的信息是我当前的代码的相关部分(其中正常工作):为什么这个“对于类型测试模式”失败?
let ctorFlags = BindingFlags.NonPublic ||| BindingFlags.Public ||| BindingFlags.Instance ||| BindingFlags.Static
let methodFlags = BindingFlags.DeclaredOnly ||| ctorFlags
[
for t in Assembly.GetExecutingAssembly().GetTypes() do
for c in t.GetConstructors ctorFlags -> c :> MethodBase
for m in t.GetMethods methodFlags -> m :> MethodBase
]
|> printfn "%A"
然后我想使使用的事实syntax是for pattern in expr
一个小的变化。并且,如果给定的输入匹配到type test pattern匹配(或派生类型的)给定类型;所以我写了这个:
// same flags as before
[
for t in Assembly.GetExecutingAssembly().GetTypes() do
for :? MethodBase as m in t.GetConstructors ctorFlags -> m
for :? MethodBase as m in t.GetMethods methodFlags -> m
]
|> printfn "%A"
这给了我一个错误的GetConstructors
线(由我翻译成英文)
不兼容的类型约束。类型
MethodBase
与类型ConstructorInfo
不兼容。
经过仔细检查ConstructorInfo源自MethodBase(并且与MethodInfo相同)。
注意:如果使用柔性类型(#MethodBase
)代替;该模式的工作原理,但对构造m
的类型为RuntimeConstructorInfo
和方法m
散列类型RuntimeMethodInfo
(什么是使用灵活型预期的行为)。我明显地测试了它们,因为有两种不同类型的列表是不允许的。
所以问题是:为什么我错过了/误解?
如果你使用:中>代替>,这是否解决您的问题? – Foole
@Fole我不使用?>(甚至不知道它存在),以防万一你的意思是替换:?与:>不能在一个模式中完成,所以只有我可以使用的地方:>将在for的“body”中,这正是我在最初的代码中完成的。如果你不是那个意思,那我就不明白了。 – Sehnsucht