我试图通过参数反射调用方法,我得到:
对象与目标类型不匹配
如果我调用没有参数的方法,它可以正常工作.如果我调用该方法Test("TestNoParameters"),则基于以下代码,它可以正常工作.但是,如果我打电话Test("Run"),我会得到一个例外.我的代码有问题吗?
我最初的目的是传递一系列对象,例如public void Run(object[] options)但这不起作用,我尝试了一些更简单的例如字符串而没有成功.
// Assembly1.dll
namespace TestAssembly
{
public class Main
{
public void Run(string parameters)
{
// Do something...
}
public void TestNoParameters()
{
// Do something...
}
}
}
// Executing Assembly.exe
public class TestReflection
{
public void Test(string methodName)
{
Assembly assembly = Assembly.LoadFile("...Assembly1.dll");
Type type = assembly.GetType("TestAssembly.Main");
if (type != null)
{
MethodInfo methodInfo = type.GetMethod(methodName);
if (methodInfo != null)
{
object result = null;
ParameterInfo[] …Run Code Online (Sandbox Code Playgroud) 我编写了一个日志类和一个函数,如下面的代码所示:
Log(System.Reflection.MethodBase methodBase, string message)
Run Code Online (Sandbox Code Playgroud)
每次我记录某些东西时,我也会从methodBase.Name和methodBase.DeclaringType.Name中记录类名.
我阅读了以下文章使用Get CurrentMethod,我注意到这个方法很慢.
我应该使用this.GetType()而不是System.Reflection.MethodBase,或者我应该在我的日志中手动记录类/方法名称,例如Log("ClassName.MethodName","log message")?最佳做法是什么?
我在wix中创建了一个自定义对话框页面,它有一个文本框.如果文本框为空,我想禁用安装程序的下一个按钮如果用户键入了值,则启用它.以下代码部分工作.它不会禁用下一个按钮,但除非您填写该值,否则它不会导航到下一页.我遇到的问题是,当您在编辑文本框中键入值时,不会更新下一个按钮的状态.如果我从编辑文本框中删除该值,然后单击返回上一个屏幕,然后单击下一步,则禁用下一个按钮.
<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Fragment>
<UI>
<Dialog Id="MyCustomDialog" Width="370" Height="270" Title="Custom Dialog Options">
<Control Id="Next" Type="PushButton" X="236" Y="243" Width="56" Height="17" Default="yes" Text="Next">
<Condition Action="disable">USERNAME1 = ""</Condition>
<Condition Action="enable">NOT(USERNAME1 = "")</Condition>
<Publish Event="NewDialog" Value="VerifyReadyDlg">NOT(USERNAME1 = "")</Publish>
</Control>
<Control Id="Back" Type="PushButton" X="180" Y="243" Width="56" Height="17" Text="Back">
<Publish Event="NewDialog" Value="CustomizeDlg">1</Publish>
</Control>
<Control Id="Cancel" Type="PushButton" X="304" Y="243" Width="56" Height="17" Cancel="yes" Text="Cancel">
<Publish Event="SpawnDialog" Value="CancelDlg">1</Publish>
</Control>
<Control Id="Description" Type="Text" X="25" Y="23" Width="280" Height="15" Transparent="yes" NoPrefix="yes" Text="Please type the value" />
<Control Id="UserNameText" Type="Text" …Run Code Online (Sandbox Code Playgroud) .NET编程指南声明我们不应该捕获一般异常.我假设以下代码不是很好,因为一般异常类型catch:
private object CreateObject(string classname)
{
object obj = null;
if (!string.IsNullOrEmpty(classname))
{
try
{
System.Type oType = System.Type.GetTypeFromProgID(customClass);
obj = System.Activator.CreateInstance(oType);
}
catch (Exception ex)
{
Log.Error("Unable to create instance for COM Object with class name " + classname + "\n" + ex.Message);
}
}
return obj;
}
Run Code Online (Sandbox Code Playgroud)
在下面的代码中,我捕获特定的异常但不是所有异常,然后我重新抛出异常,以防异常与非泛型异常.但是,函数"CreateInstance"可能会抛出许多异常(ArgumentNullException,ArgumentException,NotSupportedException,TargetInvocationException,MethodAccessException,MemberAccessException,InvalidComObjectException,MissingMethodException,COMException,TypeLoadException).
捕获所有其他个别例外是否可以接受?或者,还有更好的方法?
private object CreateObject(string classname)
{
object obj = null;
if (!string.IsNullOrEmpty(classname))
{
try
{
System.Type oType = System.Type.GetTypeFromProgID(customClass);
obj = System.Activator.CreateInstance(oType);
}
catch (NotSupportedException ex)
{
Log.Error("...." + ex.Message); …Run Code Online (Sandbox Code Playgroud) 是否可以根据鼠标的最后位置和当前位置获取鼠标方向(左,右,上,下)?我已经编写了代码来计算两个向量之间的角度,但我不确定它是否正确.
有人可以指出我正确的方向吗?
public enum Direction
{
Left = 0,
Right = 1,
Down = 2,
Up = 3
}
private int lastX;
private int lastY;
private Direction direction;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
lastX = e.X;
lastY = e.Y;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
double angle = GetAngleBetweenVectors(lastX, lastY, e.X, e.Y);
System.Diagnostics.Debug.WriteLine(angle.ToString());
//The angle returns a range of values from -value 0 +value
//How to get the direction from the angle?
//if (angle > ??) …Run Code Online (Sandbox Code Playgroud) 假设以下代码(请在最后一节的代码注释中阅读我的问题):
//This is my Generic Class
public class ClientRequestInfo<K, V>
{
public string Id { get; set; }
private Dictionary<K, V> parameters;
public ClientRequestInfo()
{
parameters = new Dictionary<K, V>();
}
public void Add(K key, V value)
{
parameters.Add(key, value);
}
}
public class ProcessParameters()
{
private void CreateRequestAlpha()
{
ClientRequestInfo<int, string> info = new ClientRequestInfo<int, string>();
info.Add(1, "Hello");
SynchRequest s = new SynchRequest(info);
s.Execute();
}
private void CreateRequestBeta()
{
ClientRequestInfo<int, bool> info = new ClientRequestInfo<int, bool>();
info.Add(1, true);
SynchRequest …Run Code Online (Sandbox Code Playgroud) 我想在C#中结合两个相对路径。
例如:
string path1 = "/System/Configuration/Panels/Alpha";
string path2 = "Panels/Alpha/Data";
Run Code Online (Sandbox Code Playgroud)
我想回来
string result = "/System/Configuration/Panels/Alpha/Data";
Run Code Online (Sandbox Code Playgroud)
我可以通过拆分第二个数组并在for循环中对其进行比较来实现此目的,但我想知道是否存在类似于Path.Combine可用的东西,或者是否可以使用正则表达式或Linq完成?
谢谢
我已经阅读了几篇F#教程,我注意到与C#相比,在F#中执行异步和并行编程是多么容易.因此,我正在尝试编写一个将从C#调用的F#库,并将C#函数(委托)作为参数并以异步方式运行.
到目前为止我已经设法传递了这个函数(我甚至可以取消)但是我想念的是如何实现回调到C#的回调,一旦异步操作完成就会执行它.(例如函数AsynchronousTaskCompleted?).另外我想知道我是否可以从函数AsynchronousTask发布(例如Progress%)回F#.
有人可以帮帮我吗?
这是我到目前为止编写的代码(我不熟悉F#所以以下代码可能是错误的或执行不当).
//C# Code Implementation (How I make the calls/handling)
//Action definition is: public delegate void Action();
Action action = new Action(AsynchronousTask);
Action cancelAction = new Action(AsynchronousTaskCancelled);
myAsyncUtility.StartTask2(action, cancelAction);
Debug.WriteLine("0. The task is in progress and current thread is not blocked");
......
private void AsynchronousTask()
{
//Perform a time-consuming task
Debug.WriteLine("1. Asynchronous task has started.");
System.Threading.Thread.Sleep(7000);
//Post progress back to F# progress window?
System.Threading.Thread.Sleep(2000);
}
private void AsynchronousTaskCompleted(IAsyncResult asyncResult)
{
Debug.WriteLine("2. The Asynchronous task has been completed - …Run Code Online (Sandbox Code Playgroud) c# ×6
reflection ×2
.net ×1
asynchronous ×1
callback ×1
controls ×1
coordinates ×1
direction ×1
f# ×1
generics ×1
invoke ×1
methodbase ×1
methods ×1
mouse ×1
parameters ×1
path ×1
performance ×1
position ×1
regex ×1
wix ×1
workflow ×1