C# 如何在C中的另一个类中使用没有构造函数的类的方法#

C# 如何在C中的另一个类中使用没有构造函数的类的方法#,c#,mono,raspbian,raspberry-pi2,C#,Mono,Raspbian,Raspberry Pi2,我正在Mono上使用Libbcm2835.Net库@。我需要调用Bcm2835类的方法,该类在库中给出,如下所示: using System; using System.Runtime.InteropServices; namespace LibBcm2835.Net { public sealed partial class Bcm2835 { //please note: I added the constructor below and tried to use it to /

我正在Mono上使用Libbcm2835.Net库@。我需要调用Bcm2835类的方法,该类在库中给出,如下所示:

using System;
using System.Runtime.InteropServices;

namespace LibBcm2835.Net
{
  public sealed partial class Bcm2835
  {
//please note: I added the constructor below and tried to use it to 
//instantiate the object to be used in ADS1256.cs, but to no avail. I tried: 
// Bcm2835 bcm2835 = new Bcm2835(1); And then invoking methods from bcm2835. 
//But that gives me "An object reference is needed to access the non-static 
//member ADS1256.bcm2835"
    public Bcm2835(int a ) { }  
    ....all the methods here-on being non-static...
  }
}
`
我现在想在另一个类ADS1256.cs中使用它,如下所示:

using System;
using LibBcm2835.Net;
namespace ForceSeer2
{
    public class ADS1256
    {       

        public ADS1256()
        {
        }


         static Bcm2835 instance;
        public static Bcm2835 Instance {
            get { return instance;}
        }
      //example of a method in this class that uses a method from the class 
      //Bcm2835:
        public static int initializeSPI ()
            {
              //if i had done Bcm2835 bcm2835 = new Bcm2835(1); at the top, 
             //i would now do : if (bcm2835.bcm2835_init ()==0) instead of 
              //the upcoming line, leading then to the error: "An object 
             //reference is needed to access the non-static 
             //member ADS1256.bcm2835"
                if (Instance.bcm2835_init ()==0)
                    return -1;
                else {
                    Instance.bcm2835_spi_begin ();
                    Instance.bcm2835_spi_setBitOrder ((byte)Bcm2835.bcm2835SPIBitOrder.BCM2835_SPI_BIT_ORDER_LSBFIRST);
                    Instance.bcm2835_spi_setDataMode ((byte)Bcm2835.bcm2835SPIMode.BCM2835_SPI_MODE1);  //whats this about??
                    Instance.bcm2835_spi_setClockDivider ((ushort)Bcm2835.bcm2835SPIClockDivider.BCM2835_SPI_CLOCK_DIVIDER_256);//whats this about??
                    Instance.bcm2835_gpio_fsel ((byte)SPICS, (byte)Bcm2835.bcm2835FunctionSelect.BCM2835_GPIO_FSEL_OUTP);//whats this about??
                    Instance.bcm2835_gpio_set ((byte)SPICS);//sets SPICS to HIGH
                    Instance.bcm2835_gpio_fsel ((byte)DRDY, (byte)Bcm2835.bcm2835FunctionSelect.BCM2835_GPIO_FSEL_INPT);//whats this about??
                    Instance.bcm2835_gpio_set_pud ((byte)DRDY, (byte)Bcm2835.bcm2835PUDControl.BCM2835_GPIO_PUD_UP); //whats this about??


                    return 1;
                }
            }
这种用法在以下行中给出了NullReferencePointer异常:

if (Instance.bcm2835_init ()==0)

这是什么原因?如何更正它?

在调用bcm2835_Init方法之前,应该添加以下代码:

// Blinks on RPi Plug P1 pin 7 (which is GPIO 4)
const Byte pin = (byte)Bcm2835.RPiGPIOPin.RPI_V2_GPIO_P1_07;

// Will extract (from embedded resource) and compile the library 
// if it doesn't exist in the same directory as LibBcm2835.dll.
Bcm2835.ExtractAndCompileLibraryIfNotExists();

// Grabbing the instance will dynamically load the libbcm2835.so 
// library, so make sure it's there before accessing this property.
Bcm2835 bcm2835 = Bcm2835.Instance;

您似乎可以使用Singleton设计模式:

using System;
using LibBcm2835.Net;
namespace ForceSeer2
{
    public class ADS1256
    {       
        public ADS1256()
        {
        }

        private static Bcm2835 instance;
        private static object syncRoot = new Object();

        public static Bcm2835 Instance
        {
            get
            {
                if(instance == null)
                {
                    lock(syncRoot)
                    {
                        if(instance == null)
                            instance = new Bcm2835();
                    }
                }

                return instance;
            }
        }

        //example of a method in this class that uses a method from the class 
        //Bcm2835:
        public static int initializeSPI ()
        {
             //Your method implementation here
        }

        //Other class members here
    }
}

您希望使用和对象而无需对其进行事件初始化。为什么?是什么阻止你使用这个类的构造函数来创建它的对象呢?@ChetanRanpariya实际上那里没有构造函数。我插入它并试图调用它。然而,它给出了同样的例外“事实上那里没有构造函数”——你在说什么?您发布的代码显示了一个构造函数。只需将
int
值传递给它即可。坦率地说,你的问题毫无意义……要调用非静态方法,你必须创建一个实例。因此,创建一个实例。如果这不起作用,你需要更好地解释为什么不起作用。@PeterDuniho你指的是这行:Bcm2835 Bcm2835=new Bcm2835(1);然后从bcm2835调用方法。但这给了我“访问非静态成员ADS1256.bcm2835需要对象引用”您发布的代码没有显示类的任何此类成员
ADS2156
,因此您描述的错误消息毫无意义。如果你需要帮助,你需要发布一个更好的问题,并清楚地显示与该问题相关的所有内容。但这只在main()中编译。我的ADS1256.cs是一个服务类。您在尝试编译它时看到了什么错误?在我看来,这段代码是我刚刚更新问题的唯一方法,在我提到的构造函数上方的注释中包含错误。它们可能需要也可能不需要单例模式,但上面的代码无法编译。不是很有用。