0

我正在检查该代码,位于https://github.com/mono/mono/blob/master/mono/metadata/security-core-clr.c

如果“确保指定的方法可以与反射一起使用,因为透明代码不能调用关键方法”对我来说很好,为什么 CoreCLR 还阻止透明代码通过反射调用内部透明方法或属性?!

有关 CoreCLR 的更多详细信息:http ://www.mono-project.com/Moonlight2CoreCLR 。

/*
 * mono_security_core_clr_ensure_reflection_access_method:
 *
 *  Ensure that the specified method can be used with reflection since
 *  Transparent code cannot call Critical methods and can only call them
 *  if they are visible from it's point of view.
 *
 *  A MethodAccessException is thrown if the field is cannot be accessed.
 */
void
mono_security_core_clr_ensure_reflection_access_method (MonoMethod *method)
{
    MonoMethod *caller = get_reflection_caller ();
    /* CoreCLR restrictions applies to Transparent code/caller */
    if (mono_security_core_clr_method_level (caller, TRUE) != MONO_SECURITY_CORE_CLR_TRANSPARENT)
        return;

    if (mono_security_core_clr_get_options () & MONO_SECURITY_CORE_CLR_OPTIONS_RELAX_REFLECTION) {
        if (!mono_security_core_clr_is_platform_image (method->klass->image))
            return;
    }

    /* Transparent code cannot invoke, even using reflection, Critical code */
    if (mono_security_core_clr_method_level (method, TRUE) == MONO_SECURITY_CORE_CLR_CRITICAL) {
        mono_raise_exception (get_method_access_exception (
            "Transparent method %s cannot invoke Critical method %s.", 
            caller, method));
    }

    /* also it cannot invoke a method that is not visible from it's (caller) point of view */
    if (!check_method_access (caller, method)) {
        mono_raise_exception (get_method_access_exception (
            "Transparent method %s cannot invoke private/internal method %s.", 
            caller, method));
    }
}
4

1 回答 1

0

在 mono-project 的讨论列表中回答:

在 Silverlight 中,反射与静态编译的代码一样强大。换句话说,如果代码由于成员访问冲突而无法编译,那么反射也无济于事。

虽然这条规则没有增加具体的安全性,但它有助于从应用程序代码中隐藏一些东西,而不必用 [SecurityCritical] 装饰成员。

http://mono.1490590.n4.nabble.com/Why-does-Mono-s-CoreCLR-blocks-internal-method-reflection-from-transparent-code-tt4659140.html;cid=1367365433900-162

于 2013-04-30T23:45:21.243 回答