- Home /
Possibly fake errors in Unity
Hello,
I have a script for which I am getting the three errors below in Unity. However, they don't show up in Visual Studio which usually spots them.
I have tried to reimport all assets of the project, but I am still getting the errors.
Assets/Comparers/PropertyComparer.cs(28,27): error CS1729: The type `Comparers.PropertyComparer<T>' does not contain a constructor that takes `2' arguments
Assets/Comparers/PropertyComparer.cs(64,25): error CS0103: The name `ValidateProperty' does not exist in the current context
Assets/Comparers/PropertyComparer.cs(80,25): error CS0103: The name `ValidateProperty' does not exist in the current context
And here is the complete code:
using System;
using System.Collections.Generic;
using System.Reflection;
namespace Comparers
{
/// <summary>
/// Defines the directions in which collection items may be sorted.
/// </summary>
public enum SortDirection : byte
{
Ascending,
Descending
}
/// <summary>
/// Supports the comparison of two objects based on a specific property.
/// </summary>
public class PropertyComparer<T> : Comparer<T>
{
private readonly PropertyInfo property;
private readonly SortDirection direction;
/// <summary>
/// Constructor.
/// </summary>
public PropertyComparer()
: this(string.Empty, SortDirection.Ascending)
{
}
/// <summary>
/// Constructor.
/// </summary>
public PropertyComparer(string propertyName, SortDirection direction = SortDirection.Ascending)
{
Type type = typeof(T);
if (string.IsNullOrEmpty(propertyName))
{
PropertyInfo[] properties = type.GetProperties();
if (properties.Length > 0)
{
property = properties[0];
}
else
{
throw new MissingMemberException(string.Format("{0} does not contain any properties", type.Name));
}
}
else
{
property = type.GetProperty(propertyName);
if (property == null)
{
throw new MissingMemberException(string.Format("{0} does not contain a property named \"{1}\"", type.Name, propertyName));
}
}
this.direction = direction;
ValidateProperty(type, false);
}
/// <summary>
/// Constructor.
/// </summary>
public PropertyComparer(PropertyInfo propertyInfo, SortDirection direction = SortDirection.Ascending)
{
if (propertyInfo == null)
{
throw new ArgumentNullException("propertyInfo");
}
this.property = propertyInfo;
this.direction = direction;
ValidateProperty(typeof(T), true);
}
/// <summary>
/// Compares two objects based on a specific property.
/// </summary>
public override int Compare(T x, T y)
{
IComparable a = property.GetValue(x, null) as IComparable;
IComparable b = property.GetValue(y, null) as IComparable;
int result = a.CompareTo(b);
if (direction == SortDirection.Descending)
{
result *= -1;
}
return result;
}
/// <summary>
/// Validates whether the property used for this comparison is valid.
/// </summary>
private void ValidateProperty(Type type, bool checkOwner)
{
if (checkOwner && property.DeclaringType != type)
{
throw new MissingMemberException(string.Format("Property {0} is not a member of {1}", property.Name, type.Name));
}
if (!property.CanRead)
{
throw new MemberAccessException(string.Format("Property {0} of {1} can't be read", property.Name, type.Name));
}
}
}
}
Any pointers on how to solve this are appreciated!
Credits for the original code: https://bitbucket.org/KornnerStudios/ksoft/src/afc1d080fc5540f9d2d4d08f80cfe5c9d152f31c/KSoft/Collections/PropertyComparer.cs?at=default&fileviewer=file-view-default
Answer by Zak_33 · Mar 14, 2016 at 11:03 PM
I found the reason for the errors: the Unity compiler does not support optional parameters on constructors. Upon removing them, the code compiles properly. I guess it's a Mono-related thing so there's nothing we could do...
Your answer
