在反射中传递参数 Windows store Apps
Passing Parameters in reflections Windows store Apps
我想将一些参数传递给在通用 windows 应用程序中使用反射调用的函数。下面是我尝试过的代码,但出现异常 "Parameters Count mismatch."。请多多指教
public class myClass
{
public async void btn_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
Type type = Type.GetType("moto_Windows.Actions.NextViewAction");
object[] mParam = new object[] { 5, 10 };
var nva = Activator.CreateInstance(type);
await (dynamic)type.GetTypeInfo().GetDeclaredMethod("NextView").Invoke(nva, mParam);
}
}
我尝试调用的 class 如下所示
namespace moto_Windows.Actions
{
public class NextViewAction
{
public NextViewAction(object [] obj)
{
//Constructor
}
public async void NextView()
{
//Method to be invoked.
}
}
}
最后我通过构造函数在字典中传递值解决了这个问题,我的代码是
Type type = Type.GetType("moto.Actions." + ctrl.Action.name);
ctrl = (Carrot_Control)btn.DataContext;
Dictionary<string, object> _dict = new Dictionary<string, object>();
dict.Add("a", 5);
_dict.Add("b", 10);
var t = Activator.CreateInstance(type, _dict);
await (dynamic)type.GetTypeInfo().GetDeclaredMethod("NextView").Invoke(t, null);
和我尝试调用的class
namespace moto_Windows.Actions
{
public class NextViewAction
{
public NextViewAction(Dictionary<string,object> _dict)
{
//Constructor
string a = _dict[a];
string b = _dict[b];
}
public async void NextView()
{
//Method to be invoked.
}
}
}
如果有人有更好的答案,请post。
我想将一些参数传递给在通用 windows 应用程序中使用反射调用的函数。下面是我尝试过的代码,但出现异常 "Parameters Count mismatch."。请多多指教
public class myClass
{
public async void btn_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
Type type = Type.GetType("moto_Windows.Actions.NextViewAction");
object[] mParam = new object[] { 5, 10 };
var nva = Activator.CreateInstance(type);
await (dynamic)type.GetTypeInfo().GetDeclaredMethod("NextView").Invoke(nva, mParam);
}
}
我尝试调用的 class 如下所示
namespace moto_Windows.Actions
{
public class NextViewAction
{
public NextViewAction(object [] obj)
{
//Constructor
}
public async void NextView()
{
//Method to be invoked.
}
}
}
最后我通过构造函数在字典中传递值解决了这个问题,我的代码是
Type type = Type.GetType("moto.Actions." + ctrl.Action.name);
ctrl = (Carrot_Control)btn.DataContext;
Dictionary<string, object> _dict = new Dictionary<string, object>();
dict.Add("a", 5);
_dict.Add("b", 10);
var t = Activator.CreateInstance(type, _dict);
await (dynamic)type.GetTypeInfo().GetDeclaredMethod("NextView").Invoke(t, null);
和我尝试调用的class
namespace moto_Windows.Actions
{
public class NextViewAction
{
public NextViewAction(Dictionary<string,object> _dict)
{
//Constructor
string a = _dict[a];
string b = _dict[b];
}
public async void NextView()
{
//Method to be invoked.
}
}
}
如果有人有更好的答案,请post。