【问题标题】:'GeneralInformation': cannot reference a type through an expression; try 'WebQuoter.Models.Chiller.GeneralInformation' instead'GeneralInformation':不能通过表达式引用类型;改用“WebQuoter.Models.Chiller.GeneralInformation”
【发布时间】:2016-02-10 12:07:18
【问题描述】:

我有以下型号:

namespace WebQuoter.Models
{
    public class Chiller
    {
        public class GeneralInformation
        {
            public string AssemblyID { get; set; }
            public string Model { get; set; }
            public string Series { get; set; }
            public string Revision { get; set; }
            public string Application { get; set; }
            public string PreviousAssyID { get; set; }
            public bool AvailbleInQuoter { get; set; }
            public bool NeedsUpdates { get; set; }
            public bool Obsolete { get; set; }
            public string EngineeringWorkRequestNo { get; set; }
            public string Customer { get; set; }
            public string CustomerMachineModel { get; set; }
            public string CustomerPartNumber { get; set; }
            public string PrimaryVoltage { get; set; }
            public string SecondaryVoltage { get; set; }
            public string GeneralNotes { get; set; }
            public string ChillerNotes { get; set; }
        }
        public class Cooling
        {
            public string Refrigerant { get; set; }
            public string RefrigerantCapacity { get; set; }
            public string CompressorType { get; set; }
            public string CompressorHP { get; set; }
            public string CondenserType { get; set; }
            public string Fan { get; set; }
        }
        public class FluidCircuit
        {
            public class PrimaryCircuit
            {
                public string CoolingCapacity { get; set; }
                public string FlowRate { get; set; }
                public string PumpPressure { get; set; }
            }
            public class SecondaryCircuit
            {
                public string CoolingCapacity { get; set; }
                public string FlowRate { get; set; }
                public string PumpPressure { get; set; }
            }
            public string FluidTemperature { get; set; }
            public string TemperatureStability { get; set; }
            public string SensorLocation { get; set; }
            public string PumpHorsePower { get; set; }
            public string CoolingMedium { get; set; }
            public string TankCapacity { get; set; }
            public string TankMaterialOfConstruction { get; set; }
            public string TankType { get; set; }
            public string FluidConnections { get; set; }
            public string FluidConnectionsSize { get; set; }
            public string FluidConnectionsUnitOfMeasure { get; set; }
            public string WettedComponentMaterial { get; set; }
            public string Evaporator { get; set; }
            public string GaugeType { get; set; }
        }
        public class ClimateData
        {
            public string SiteOfInstallation { get; set; }
            public string AmbientConditions { get; set; }
            public string SoundLevel { get; set; }
        }
        public class Controller
        {
            public class PrimaryVoltage
            {
                public string Voltage { get; set; }
                public string Phases { get; set; }
                public string Frequency { get; set; }
                public string ControlVoltage { get; set; }
            }
            public class SecondaryVoltage
            {
                public string Voltage { get; set; }
                public string Phases { get; set; }
                public string Frequency { get; set; }
            }
            public class CertificationsStandards
            {
                public string UL1995 { get; set; }
                public string CE { get; set; }
                public string NFPA79 { get; set; }
                public string CSA { get; set; }
                public string UL508A { get; set; }
                public string AdditionalCertifications { get; set; }
            }
            public string FullLoadedAmps { get; set; }
            public string DisconnectFuseSize { get; set; }
            public string FalseSignalAlarm { get; set; }
            public string ControllerType { get; set; }
        }
        public class AdditionalData
        {
            public string WidthAirInletSideWithIncludedOptions { get; set; }
            public string DepthWithIncludedOptions { get; set; }
            public string HeightWithIncludedOptions { get; set; }
            public string Finish { get; set; }
            public string CratedWeight { get; set; }
            public string WetWeight { get; set; }
            public string ExportWeight { get; set; }
        }
        public class Features
        {
            public string BypassType { get; set; }
            public string FluidFilter { get; set; }
            public string FlowSwitch { get; set; }
            public string FluidLevelSwitch { get; set; }
            public string AutoMakeUp { get; set; }
            public string CapacityControl { get; set; }
            public string AntiBackFlow { get; set; }
            public string FluidHeater { get; set; }
            public string RefrigerantSightGlass { get; set; }
            public string AmbientTracking { get; set; }
            public string FeetCasters { get; set; }
            public string FillPort { get; set; }
            public string TankLevelSightGlass { get; set; }
            public string RefrigerantFilterDryer { get; set; }
            public string AirFilter { get; set; }
            public string WireConnection { get; set; }
            public string RemoteControls { get; set; }
            public string SpecialFeatures { get; set; }
            public string NominalCondensorSize { get; set; }
            public string FluidSetPointRange { get; set; }
            public string NominalCoilSize { get; set; }
            public string NomalBTUPerHour { get; set; }
            public string CabinetType { get; set; }
            public string Tank { get; set; }
            public string SaltwaterPackage { get; set; }
            public string HeaderSize { get; set; }
            public string PanelType { get; set; }
            public string SwitchoverType { get; set; }
            public string Pump { get; set; }
            public string PumpCurve { get; set; }
            public string HeatRejectionType { get; set; }
            public string NumberOfFluidCircuits { get; set; }
        }
    }
}

以及访问此模型的以下控制器代码:

using Jitbit.Utils;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using WebQuoter.Models;

namespace WebQuoter.BusinessLayer
{

        public Chiller ParseValues(List<string> Values)
        {
            Chiller retVal = new Chiller();

            foreach(string value in Values)
            {
                string[] seperated = Regex.Split(value, "{EPDMSEPERATOR}");

                switch (seperated[0])
                {
                    case "07":
                        retVal.GeneralInformation.AssemblyID = "";
                        break;
                }
            }

            return retVal;
        }
    }
}

在下面一行:

retVal.GeneralInformation.AssemblyID = "";

我得到错误:

'GeneralInformation':不能通过表达式引用类型; 改用“WebQuoter.Models.Chiller.GeneralInformation”。

我所看到的所有地方都在说不要使用关键字静态。我没有在任何地方使用对这个自定义类的静态引用。

非常感谢任何帮助!

【问题讨论】:

  • 您已经创建了嵌套类,但 Chiller 不包含任何 GeneralInformation 实例。您需要创建一个包含GeneralInformation 实例的属性才能访问它,如果它不是静态的,则不能在没有实例的类中粘贴信息。

标签: c# model-view-controller types reference expression


【解决方案1】:

这里的问题是你有嵌套类但没有实例,例如:

public class TopLevel
{
    public class InsideClass
    {
        public string SomeProperty { get; set; }
    }
}

除非InsideClassstatic,否则您无法访问SomeProperty。你需要创建一个类的实例:

public class TopLevel
{
    public class InsideClass
    {
        public string SomeProperty { get; set; }
    }
    public InsideClass MyInsideClass { get; set; }

    public TopLevel()
    {
        MyInsideClass = new InsideClass();
    }
}

现在您可以通过实例访问SomeProperty

var myOutside = new TopLevel();
myOutside.MyInsideClass.SomeProperty = "Some Value";

创建嵌套类不会自动使嵌套类成为外部类的Member

【讨论】:

  • 谢谢,效果很好。我对每个嵌套类都进行了一些编辑,但它奏效了!
猜你喜欢
  • 1970-01-01
  • 2022-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-16
  • 1970-01-01
相关资源
最近更新 更多