我正在尝试将一个简单的对象写入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) 我想在我的应用程序中显示我的应用程序版本号,最简单的方法是使用程序集的版本号.
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()'失败.
为什么会发生这种情况,有什么我可以做的,如果没有,还有其他方法来检索汇编版本吗?
我已A.Test()
声明public virtual
并B.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 …