就C#而言,我是一个初学者,我完全不知道这里的错误是什么......
using System;
using System.Windows.Forms;
namespace Sancarn
{
public class Form1 : Form
{
public event EventHandler MessageHandler;
public Message lastMessage;
public string ptrToString(ptr As IntPtr)
{
return Marshal.PtrToStringAnsi(ptr);
}
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
protected override void WndProc(ref Message m){
EventHandler handler = MessageHandler;
lastMessage = m;
if(null != MessageHandler) MessageHandler(this,EventArgs.Empty);
base.WndProc(ref m);
}
}
}
当我尝试在powershell中将其添加为新类型时,我收到以下错误:
Add-Type : c:\Users\sancarn\AppData\Local\Temp\vqezctgu.0.cs(11) : ) expected
c:\Users\sancarn\AppData\Local\Temp\vqezctgu.0.cs(10) :
c:\Users\sancarn\AppData\Local\Temp\vqezctgu.0.cs(11) : >>> public string ptrToString(ptr As IntPtr)
c:\Users\sancarn\AppData\Local\Temp\vqezctgu.0.cs(12) : {
I...
Add-Type : c:\Users\sancarn\AppData\Local\Temp\vqezctgu.0.cs(11) : Invalid token ')' in class, struct, or interface member declaration
c:\Users\sancarn\AppData\Local\Temp\vqezctgu.0.cs(10) :
c:\Users\sancarn\AppData\Local\Temp\vqezctgu.0.cs(11) : >>> public string ptrToString(ptr As IntPtr)
c:\Users\sancarn\AppData\Local\Temp\vqezctgu.0.cs(12) : {
...
Add-Type : c:\Users\sancarn\AppData\Local\Temp\vqezctgu.0.cs(13) : Invalid token '(' in class, struct, or interface member declaration
c:\Users\sancarn\AppData\Local\Temp\vqezctgu.0.cs(12) : {
c:\Users\sancarn\AppData\Local\Temp\vqezctgu.0.cs(13) : >>> return Marshal.PtrToStringAnsi(ptr);
c:\Users\sancarn\AppData\Local\Temp\vqezctgu.0.cs(14) : }
...
Add-Type : c:\Users\sancarn\AppData\Local\Temp\vqezctgu.0.cs(13) : Invalid token ')' in class, struct, or interface member declaration
c:\Users\sancarn\AppData\Local\Temp\vqezctgu.0.cs(12) : {
c:\Users\sancarn\AppData\Local\Temp\vqezctgu.0.cs(13) : >>> return Marshal.PtrToStringAnsi(ptr);
c:\Users\sancarn\AppData\Local\Temp\vqezctgu.0.cs(14) : }
...
Add-Type : c:\Users\sancarn\AppData\Local\Temp\vqezctgu.0.cs(17) : Expected class, delegate, enum, interface, or struct
c:\Users\sancarn\AppData\Local\Temp\vqezctgu.0.cs(16) : [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand,
Name="FullTrust")]
c:\Users\sancarn\AppData\Local\Temp\vqezctgu.0.cs(17) : >>> protected override void WndProc(ref Message m){
c:\Users\sancarn\AppData\Local\Temp\vqezctgu.0.cs(18) : EventHandler handler = MessageHandler;
...
Add-Type : c:\Users\sancarn\AppData\Local\Temp\vqezctgu.0.cs(26) : Type or namespace definition, or end-of-file expected
c:\Users\sancarn\AppData\Local\Temp\vqezctgu.0.cs(25) : }
c:\Users\sancarn\AppData\Local\Temp\vqezctgu.0.cs(26) : >>> }
...
Add-Type : c:\Users\sancarn\AppData\Local\Temp\vqezctgu.0.cs(16) : The modifier 'override' is not valid for this item
c:\Users\sancarn\AppData\Local\Temp\vqezctgu.0.cs(15) :
c:\Users\sancarn\AppData\Local\Temp\vqezctgu.0.cs(16) : >>> [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand,
Name="FullTrust")]
c:\Users\sancarn\AppData\Local\Temp\vqezctgu.0.cs(17) : protected override void WndProc(ref Message m){
...
所以我不确定这里发生了什么。从我的角度来看,所有的括号都是正确配对的...我已经弄清楚,如果我注释掉ptrToString
声明,那么这个类就编译得很好......所以这让我相信这个声明是以某种方式归咎于这个错误,但是...要么我失明了,要么失去了一些大的东西,要么这个陈述没有错?......
public string ptrToString(ptr As IntPtr){
return Marshal.PtrToStringAnsi(ptr);
}
答案 0 :(得分:3)
在C#中,您不使用As
声明方法参数 - 即VB。
这是固定代码:
using System;
using System.Windows.Forms;
namespace Sancarn {
public class Form1 : Form {
public event EventHandler MessageHandler;
public Message lastMessage;
public string ptrToString(System.IntPtr ptr) {
return Marshal.PtrToStringAnsi(ptr);
}
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
protected override void WndProc(ref Message m) {
EventHandler handler = MessageHandler;
lastMessage = m;
if (null != MessageHandler) MessageHandler(this, EventArgs.Empty);
base.WndProc(ref m);
}
}
}