我遇到了前面提到的非常简单的错误。我以为我会很快解决它,但是即使经过一番搜索,我仍然无法一生找出问题所在。我有以下界面:
public interface ITemperatureEmitter
{
float CurrentTemperatureAddon { get; }
}
我在另外两个(暂时为空)接口中实现了这一点:
public interface ITemperatureEmitterEnvironment : ITemperatureEmitter
public interface ITemperatureEmitterSphere : ITemperatureEmitter
随后,我在以下类中使用这三个接口:
using System.Collections.Generic;
using UnityEngine;
public class TemperatureReceiver : MonoBehaviour, ITemperatureReceiver
{
public float PerceivedTemperature;
// Serialized for debug purposes
[SerializeField] private List<ITemperatureEmitterSphere> temperatureEmitterSpheres;
[SerializeField] private List<ITemperatureEmitterEnvironment> temperatureEmitterEnvironments;
[SerializeField] private float environmentTemperature;
[SerializeField] private float temperatureToModifyBy;
[SerializeField] private float currentTemperatureAddon;
[SerializeField] private float appliedTemperatureAddon;
[SerializeField] private float totalTemperatureAddon;
private void Update()
{
UpdatePerceivedTemperature();
}
private void OnTriggerEnter(Collider other)
{
if (other.GetComponent<ITemperatureEmitterSphere>() != null)
{
temperatureEmitterSpheres.Add(other.GetComponent<ITemperatureEmitterSphere>());
}
else if (other.GetComponent<ITemperatureEmitterEnvironment>() != null)
{
temperatureEmitterEnvironments.Add(other.GetComponent<ITemperatureEmitterEnvironment>());
}
}
private void OnTriggerExit(Collider other)
{
if (other.GetComponent<ITemperatureEmitterSphere>() != null)
{
temperatureEmitterSpheres.Remove(other.GetComponent<ITemperatureEmitterSphere>());
}
else if (other.GetComponent<ITemperatureEmitterEnvironment>() != null)
{
temperatureEmitterEnvironments.Remove(other.GetComponent<ITemperatureEmitterEnvironment>());
}
}
private void UpdatePerceivedTemperature()
{
ModifyPerceivedTemperature(temperatureEmitterSpheres);
ModifyPerceivedTemperature(temperatureEmitterEnvironments);
}
private void ModifyPerceivedTemperature<ITemperatureEmitter>(List<ITemperatureEmitter> list)
{
if (list.Count > 0)
{
foreach (var item in list)
{
currentTemperatureAddon += item.CurrentTemperatureAddon;
}
currentTemperatureAddon = currentTemperatureAddon / list.Count;
appliedTemperatureAddon = PerceivedTemperature;
temperatureToModifyBy = currentTemperatureAddon = appliedTemperatureAddon;
PerceivedTemperature += temperatureToModifyBy;
}
}
}
现在,
item.CurrentTemperatureAddon
方法中的ModifyPercievedTemperature
发出“错误CS1061:类型ITemperatureEmitter
不包含CurrentTemperatureAddon
的定义,并且找不到类型为CurrentTemperatureAddon
的扩展方法ITemperatureEmitter
。您是否缺少程序集参考?”ITemperatureEmitter
确实包含CurrentTemperatureAddonm
的定义...任何人都知道这里发生了什么吗? 最佳答案
您误报了ModifyPerceivedTemperature
private void ModifyPerceivedTemperature<ITemperatureEmitter>(List<ITemperatureEmitter> list)
在这里,您将
ITemperatureEmitter
用作通用参数的名称。因此,在此范围内,它不再意味着interface ITemperatureEmitter
,而是通用参数。只需删除通用参数,无论如何它都不会被使用:
private void ModifyPerceivedTemperature(List<ITemperatureEmitter> list)
现在,您只需使用
List<ITemperatureEmitter> list
作为参数,并且您所有的var item in list
实际上都是类型ITemperatureEmitter
,而不是泛型类型。