将动态函数调用到对象错误
我知道这个标题听起来有点奇怪,但这让我有点困惑。因此,英特尔提供了这个 TurboBoost 侧边栏小工具,可以使用 JavaScript 进行调用,我想用 C# 从头开始编写一个程序来执行相同的操作。这些调用源于我认为是我轻松导入的 ActiveX DLL。问题是,每当我尝试调用函数时,它都会给出错误“非静态字段需要对象引用...”我发现所有函数(例如我使用的函数)返回动态数据结构。我尝试过拆分这些函数并使它们都是静态的,但仍然没有成功。代码如下(ITurboBoostClient是接口部分):
namespace TurboBoostMon_CLI
{
class Program
{
public static object GetCPUFreq()
{
object n = ITurboBoostClient.GetCurBaseFrequency(); //<---- error
//return Convert.ToDouble(n);
return n;
}
public static void Main(string[] args)
{
object cpubasefreq = GetCPUFreq();
Console.WriteLine(cpubasefreq); // neglect the output for now
}
}
}
I know the title sounds a bit strange, but this has been boggling my mind for a little bit. So Intel offers this TurboBoost sidebar gadget with calls using JavaScript, and I want to write a program from scratch in C# that does the same thing. The calls stem from what I believe is an ActiveX DLL which I easily imported. The problem is, whenever I try to call a function, it gives me the error "an object reference is required for the non-static field..." I've found all of the functions e.g. the one I used return a dynamic data structure. I've tried splitting up the functions and made them both static but still no luck. Here's the code(ITurboBoostClient is the interface portion):
namespace TurboBoostMon_CLI
{
class Program
{
public static object GetCPUFreq()
{
object n = ITurboBoostClient.GetCurBaseFrequency(); //<---- error
//return Convert.ToDouble(n);
return n;
}
public static void Main(string[] args)
{
object cpubasefreq = GetCPUFreq();
Console.WriteLine(cpubasefreq); // neglect the output for now
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果使用典型的命名约定,则 ITurboBoostClient 是一个
接口
,并且您没有实现该接口的对象实例。因此,出现错误。如果不了解有关 ActiveX DLL 的更多信息,很难准确地说出要做什么,但大致如下:
请注意,在第一行中,您调用一个静态方法,该方法可以生成以下类(带有接口):必需的。然后你就可以实际使用该界面了。
再次查看您导入的 ActiveX 库,看看是否可以找到工厂方法、CreateInstance 方法或其他将创建初始对象的实例化器。
If typical naming conventions are being used, ITurboBoostClient is an
interface
, and you do not have an instance of an object that implements the interface. Hence, the error.Without knowing more about the ActiveX DLL, its hard to say exactly what to do, but it would be along the lines of:
Note that in the first line, you call a static method that can product the class (with the interface) that is required. Then you can actually use that interface.
Look again through the ActiveX library you imported, and see if you can find a factory method, a CreateInstance method, or some other instantiator that will create the initial object.
如果您收到该错误,则需要将某些内容声明为新对象。假设您的错误标记是正确的,您需要更改它以创建继承 ITurboBoostClient 的某个对象的新实例,然后使用它来调用 GetCurBaseFrequenct() 方法。
比如:
抱歉,我不知道你需要在那里实例化什么类,但是在谷歌上进行简短的挖掘肯定会有所启发。
If you're getting that error, then you need to declare something as a new object. Assuming your error marker is correct, you need to change that to create a new instance of some object that inherits the ITurboBoostClient, then use that to call the GetCurBaseFrequenct() method.
Something like:
Sorry I don't know what class you need to instantiate there, but a short dig on google will most surely be revealing.