以下是我编写的一小段代码,用于演示此问题的基础知识.
private async void Form1_Load( object sender, EventArgs e ) {
var result = await TestAsyncMethodName();
}
private async Task<string> TestAsyncMethodName() {
string name = "Method: " + System.Reflection.MethodBase.GetCurrentMethod().Name;
int x = 0;
foreach ( StackFrame sf in (new StackTrace()).GetFrames()) {
x++;
name = name + "\nStack Frame [" + x + "] : " + sf.GetMethod().Name;
}
await Task.Run( () => { name = name + "\nAnonymous Method: " + System.Reflection.MethodBase.GetCurrentMethod().Name; } );
return name;
}
Run Code Online (Sandbox Code Playgroud)
Method: …Run Code Online (Sandbox Code Playgroud) 我试图使用反射和我见过的所有例子说,这应该工作。我正在尝试获取如下所示的PortName属性。
返回null:
var port = this.GetType().GetProperty("PortName", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetProperty);
Run Code Online (Sandbox Code Playgroud)
出于好奇,我试图static final int SIZE从任何拥有它的类中打印该字段.要获得"任何拥有它的课程",我只是试图获得所有课程.但我注意到它总是返回一个长度为0的数组.
医生说
如果此Class对象没有公共成员类或接口,则此方法返回长度为0的数组.如果此Class对象表示基本类型,数组类或void,则此方法还返回长度为0的数组
但是Object没有列为原语.对象在某种程度上被视为无效吗?
那么,我该如何
- 获取所有类的数组?
和
-为什么这不是为对象工作?
在运行时,我想加载一个程序集,需要找到其依赖程序集的名称,以便我可以确定执行给定的DLL文件所需的程序集.
我想在我的应用程序中在运行时修改Assembly.Location属性.我正在从资源加载.NET程序集,并且加载的程序集(从内存加载)将此字段设置为空并且它会破坏应用程序功能.
我无法控制他们的源代码.我知道如何在我自己的代码中解决这个bug,但事实并非如此.当他们使用Assembly.Location并获得一个空字符串时,问题就开始了.
http://msdn.microsoft.com/en-us/library/system.reflection.assembly.location.aspx
这是只读的.有没有低级别的方法呢?有任何想法吗?
我的应用程序是嵌入在资源中的那些应用程序的加载器,因此它不依赖于它们.尝试从内存中加载任何程序集并检查那些已加载程序集的Assembly.Location字段,它将为空.它们没有位置,因为它们是从内存中加载的,我仍然希望通过.NET内部修改或任何其他方法来改变它.
压缩的程序集无法解压缩到磁盘.如果你只是知道如何实现它,我不介意重大问题的危险.
我上课了
private class MyRouter
{
public string Json {get;set;}
public string Class { get; set; }
public string Method { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
它必须通过Json Arg调用Class中的Method,我怎样才能通过Reflection实现它?我做了这个,但没有任何帮助
MyRouter MR = new MyRouter(){initilising the class};
Assembly assembly = Assembly.Load("Common");
Type t = assembly.GetType("Common." + MR.Class);
var x = t.GetMethod(MR.Method ).Invoke(MR.Json,null);
Run Code Online (Sandbox Code Playgroud) 我想反映MyRow的一个对象然后得到一个My MyTable对象,我该怎么办?
这是我的java代码:
import java.util.*;
import java.text.*;
import java.util.concurrent.*;
import java.lang.reflect.*;
class MyTable {
private MyRows rows;
private List columns;
public List getColumns(){return this.columns;}
}
class MyRows extends ArrayList<MyRow> {//...
}
class MyRow {
protected MyTable table;
private List<Object> data;
private MyRow(MyTable p_table) {
this.table = p_table;
this.data = Collections.synchronizedList(new ArrayList(this.table.getColumns().size()));
}
}
public class Test2 {
public static void main(String args[]){
try{
MyTable myTable = new MyTable();
MyRows myRows = new MyRows();
Constructor ctor = MyRow.class.getDeclaredConstructor(MyTable.class);
ctor.setAccessible(true);
MyRow myRow …Run Code Online (Sandbox Code Playgroud) 我正在尝试做这样的事情:
public class MyClass()
{
private void SetAValiable(int boolNumber)
{
bool b1 = false;
bool b2 = false;
("b" + boolNumber) = true;
}
}
Run Code Online (Sandbox Code Playgroud)
我试过这个但是不断从GetProperty调用中得到一个null:
Type myType = typeof(MyClass);
PropertyInfo pinfo = myType.GetProperty("b" + boolNumber);
pinfo.SetValue(myType, true, null);
Run Code Online (Sandbox Code Playgroud)
任何人都有任何想法如何让这个工作?
谢谢!
System.Reflection的文档.MemberInfo有以下示例:
foreach (MemberInfo mi in t.GetMembers())
{
if (mi.MemberType == MemberTypes.Method)
{
foreach (ParameterInfo pi in ((MethodInfo)mi).GetParameters())
Run Code Online (Sandbox Code Playgroud)
注意演员(MethodInfo)mi).MemberInfo并且MethodInfo没有共同的父母,为什么允许演员?
我发现以下代码片段让我感到困惑.
public class Bclass : Aclass
{
public const BindingFlags Flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
public Bclass(IAclass a) : base(string.Empty)
{
var destFields = this.GetType().BaseType.GetFields( Flags );
a.GetType().GetFields( Flags ).Where(x => destFields.Contains(x)).ToList().ForEach(property =>
{
destFields.First(x => x == property).SetValue(this, property.GetValue(a));
});
var destProperties = this.GetType().BaseType.GetProperties( Flags );
a.GetType().GetProperties( Flags ).Where(x => destProperties.Contains(x)).ToList().ForEach(property =>
{
destProperties.First(x => x == property).SetValue(this, property.GetValue(a, null));
});
}
// some more methods...
}
Run Code Online (Sandbox Code Playgroud)
我的主要问题是......为什么有人会想到这样做......这段代码可以带来什么好处.