属性“DllImport”在此声明类型上无效。它仅对'方法'声明有效 [英] Attribute 'DllImport' is not valid on this declaration type. It is only valid on 'method' declarations

查看:935
本文介绍了属性“DllImport”在此声明类型上无效。它仅对'方法'声明有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨...我在调用dll后得到这个(属性DllImport在此声明类型上无效。它仅对方法声明有效)错误....



请看以下代码







< pre lang =cs>使用System;

使用System.Collections.Generic;

使用System.ComponentModel;

使用System.Data ;

使用System.Drawing;

使用System.Linq;

使用System.Text;

使用System .Windows.Forms;

使用System.Data.SqlClient;

使用System.IO;

使用System.Runtime.InteropServices;

使用System.Diagnostics;

使用System.Data.OleDb;



命名空间EmpDetail

$



公共部分课程表格1:表格

{



[DllImport(AkrutiEngine.dll)]



static int a;

Hi... i m getting this(Attribute DllImport is not valid on this declaration type. It is only valid on method declarations) error after calling a dll....

please see following code



<pre lang="cs">using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Data.OleDb;

namespace EmpDetail
{

public partial class Form1 : Form
{

[DllImport("AkrutiEngine.dll")]

static int a;

推荐答案

你能真正阅读错误信息吗?这就是它。您正在尝试将 DllImport 属性应用于字段,这没有任何意义。



从技术上讲,它通过将另一个属性 System.AttributeUsageAttribute 应用于此属性来实现,以限制某些属性的目标:

http://msdn.microsoft.com/en-us/library/system.attributeusageattribute.aspx [<一个href =http://msdn.microsoft.com/en-us/library/system.attributeusageattribute.aspxtarget =_ blanktitle =新窗口> ^ ],

http://msdn.microsoft.com/en-us/library/system.attributetargets。 aspx [ ^ ]。



您可以应用属性 System.Runtime.InteropServices.DllImportAttrib ute 仅用于方法,因为您应该从错误消息中理解:

http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute.aspx [ ^ ]。



我怀疑你是否知道为什么要使用 DllImport 。要了解它,请参阅:

http://en.wikipedia.org/wiki/ P / Invoke [ ^ ],

http://msdn.microsoft.com/library/en- us / vcmxspec / html /vcmg_PlatformInvocationServices.asp [ ^ ]。



此CodeProject文章也可以使用:http://www.codeproject.com/csharp/EssentialPInvoke.asp [ ^ ]。



-SA
Can you really read the error message? This is what it is. You are trying to apply the DllImport attribute to a field, which makes no sense whatsoever.

Technically, it is implemented through application of another attribute, System.AttributeUsageAttribute to this attribute, to limit the targets of some attributes:
http://msdn.microsoft.com/en-us/library/system.attributeusageattribute.aspx[^],
http://msdn.microsoft.com/en-us/library/system.attributetargets.aspx[^].

You can apply the attribute System.Runtime.InteropServices.DllImportAttribute only to methods, as you should have understood from your error message:
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute.aspx[^].

I doubt you have a clue why using DllImport. To learn about it, please see:
http://en.wikipedia.org/wiki/P/Invoke[^],
http://msdn.microsoft.com/library/en-us/vcmxspec/html/vcmg_PlatformInvocationServices.asp[^].

This CodeProject article can also be used: http://www.codeproject.com/csharp/EssentialPInvoke.asp[^].

—SA

这篇关于属性“DllImport”在此声明类型上无效。它仅对'方法'声明有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆