首页 > 解决方案 > "No argument given" when inheriting from a class

问题描述

Unity uses an older version of C#, so we don't have a builtin Tuple class. I have the following definition that I basically copied from somewhere on the internet, unfortunately I don't remember where:

public class Tuple<T1, T2>
{
    public T1 First { get; private set; }
    public T2 Second { get; private set; }
    internal Tuple(T1 first, T2 second)
    {
        First = first;
        Second = second;
    }
}

public static class Tuple
{
    public static Tuple<T1, T2> New<T1, T2>(T1 first, T2 second)
    {
        var tuple = new Tuple<T1, T2>(first, second);
        return tuple;
    }
}

That's fine, it works for my needs.

However now I have a script which needs to refer to a specific Tuple type many times, and I want to 'alias' that type:

 public class RequiredField : Tuple<Type, MonoBehaviour>{}

I want RequiredField to basically be an alias for Tuple<Type, MonoBehaviour>.

This is giving me an error:

There is no argument given that corresponds to the required formal parameter 'first' of 'Tuple.Tuple(Type, MonoBehaviour)' (CS7036) [Assembly-CSharp]

I sort of know the bare-minimum amount of C# needed to do basic Unity programming ... unfortunately that means I am ill-equipped to debug errors like this.

Can anyone point in the direction of how to fix this?

标签: c#

解决方案


我不确定您使用的是哪种版本的 Unity,但它已经支持 .Net4.x 有一段时间了。如果没有为您的项目设置它,那么您可以在Project Settings-> Player->下设置它Scripting Runtime Version。将其设置为.Net 4.x Equivalent,您可以使用System命名空间中的元组。

项目设置

这里有图为证。

在此处输入图像描述


推荐阅读