标签: methodaccessexception

WP7.1上的匿名类型和Get访问器?

我正在尝试将一个简单的对象写入Dictionary转换器,如下所示:

public static class SimplePropertyDictionaryExtensionMethods
{
    public static IDictionary<string,string> ToSimplePropertyDictionary(this object input)
    {
        if (input == null)
            return new Dictionary<string, string>();

        var propertyInfos = from property in input.GetType()
                                .GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy | BindingFlags.GetProperty)
                            where property.CanRead
                            select property;

        return propertyInfos.ToDictionary(x => x.Name, x => input.GetPropertyValueAsString(x));
    }

    public static string GetPropertyValueAsString(this object input, PropertyInfo propertyInfo)
    {
        var value = propertyInfo.GetGetMethod().Invoke(input, new object[] {});
        if (value == null)
            return string.Empty ;

        return value.ToString();
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试将其称为:

var test = (new { …
Run Code Online (Sandbox Code Playgroud)

c# reflection windows-phone-7 methodaccessexception

10
推荐指数
1
解决办法
1237
查看次数

无法从我的Silverlight应用程序调用Assembly.GetName()

我想在我的应用程序中显示我的应用程序版本号,最简单的方法是使用程序集的版本号.

var assembly = System.Reflection.Assembly.GetExecutingAssembly();
var name = assembly.GetName();
return String.Format("Version {0}.{1}", name.Version.Major, name.Version.Minor);
Run Code Online (Sandbox Code Playgroud)

我可以毫无问题地获得执行组装,但是调用GetName()返回MethodAccessException带有此消息的a

尝试使用安全透明方法'MainPage..ctor()'来访问安全关键方法'System.Reflection.Assembly.GetName()'失败.

为什么会发生这种情况,有什么我可以做的,如果没有,还有其他方法来检索汇编版本吗?

c# reflection silverlight version methodaccessexception

6
推荐指数
2
解决办法
3346
查看次数

这个C#代码合法吗?

我已A.Test()声明public virtualB.Test()声明为private new.
我是base.Test()C那个继承人那里打来的B.

此代码使用Mono 2.10.2编译但抛出MethodAccessException:

class A {
    public virtual void Test () { }
}

class B : A {
    private new void Test () { }
}

class C : B {
    public C ()
    {
        base.Test ();
    }

    public static void Main (string[] args)
    {
        var c = new C ();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我得到的例外:

System.MethodAccessException: Method TestBug.B:Test () is inaccessible …

.net c# mono c#-5.0 methodaccessexception

4
推荐指数
1
解决办法
295
查看次数