C# 在Windows应用商店应用程序中传递参数
我想将一些参数传递给在通用windows应用程序中使用反射调用的函数。下面是我尝试的代码,我得到一个异常“参数计数不匹配”。请告诉我C# 在Windows应用商店应用程序中传递参数,c#,windows-phone-8,reflection,windows-8,windows-runtime,C#,Windows Phone 8,Reflection,Windows 8,Windows Runtime,我想将一些参数传递给在通用windows应用程序中使用反射调用的函数。下面是我尝试的代码,我得到一个异常“参数计数不匹配”。请告诉我 public class myClass { public async void btn_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) { Type type = Type.GetType("moto_Windows.Actions.NextViewAction");
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);
}
}
我试图调用的类如下所示
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);
Type Type=Type.GetType(“moto.Actions.+ctrl.Action.name”);
ctrl=(胡萝卜控制)btn.DataContext;
字典_dict=新字典();
添加(“a”,5);
_添加(“b”,第10条);
var t=Activator.CreateInstance(类型,_dict);
wait(动态)type.GetTypeInfo().GetDeclaredMethod(“NextView”).Invoke(t,null);
以及我试图调用的类
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.
}
}
}
namespace moto\u Windows.Actions
{
公共类下一次查看操作
{
公共下一步行动(字典)
{
//建造师
字符串a=_dict[a];
字符串b=_dict[b];
}
公共异步void NextView()
{
//要调用的方法。
}
}
}
如果有人有更好的答案,请发帖。最后我通过构造函数传递字典中的值来解决这个问题,我的代码是
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);
Type Type=Type.GetType(“moto.Actions.+ctrl.Action.name”);
ctrl=(胡萝卜控制)btn.DataContext;
字典_dict=新字典();
添加(“a”,5);
_添加(“b”,第10条);
var t=Activator.CreateInstance(类型,_dict);
wait(动态)type.GetTypeInfo().GetDeclaredMethod(“NextView”).Invoke(t,null);
以及我试图调用的类
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.
}
}
}
namespace moto\u Windows.Actions
{
公共类下一次查看操作
{
公共下一步行动(字典)
{
//建造师
字符串a=_dict[a];
字符串b=_dict[b];
}
公共异步void NextView()
{
//要调用的方法。
}
}
}
如果有人有更好的答案,请发帖。您的构造函数有必需的参数,您不能传递它们。然后对于这个方法,当这个方法没有任何参数时,你在传递参数。你的构造函数有必需的参数,而你没有传递它们。然后对于该方法,当该方法没有任何参数时,传递参数。