C
C#3y ago
Xan.Nava

How to call a static method form interface in a generic class.

So I have a class as bellow.
public interface ISetup<T, P> where T : class {
public static T /*.*/ Setup(T target, P args) {
return target;
}
}
public interface ISetup<T, P> where T : class {
public static T /*.*/ Setup(T target, P args) {
return target;
}
}
and then a second generic class.
public class ClassName<T, P> where T : class, ISetup<T, P>, new() {
public T MethodName(P setupArg) {
var holder = new T();
return ISetup<T, P>.Setup(holder, setupArg);
}
}
public class ClassName<T, P> where T : class, ISetup<T, P>, new() {
public T MethodName(P setupArg) {
var holder = new T();
return ISetup<T, P>.Setup(holder, setupArg);
}
}
but I am getting the error. https://learn.microsoft.com/en-us/dotnet/csharp/misc/cs0704?f1url=%3FappId%3Droslyn%26k%3Dk(CS0704) The problem is I can't directly call the type from a generic. Is there anyway to do this, or am I trying to do something C# just can't do?
7 Replies
Kouhai
Kouhai3y ago
Can't you do ISetup<T, P>.Setup(holder, setupArg)
Xan.Nava
Xan.NavaOP3y ago
CS0176 Member 'ISetup<TYPE, ARGS>.Setup(TYPE, ARGS)' cannot be accessed with an instance reference; qualify it with a type name instead CS0426 The type name 'Setup' does not exist in the type 'ISetup<TYPE, ARGS>'
Kouhai
Kouhai3y ago
This works on my end, what version of C# and dotnet are you using?
Xan.Nava
Xan.NavaOP3y ago
.NET Framework Very well could be. The fact Unity hasn't updated to .net 6.0 is the bain of my existence on many levels.
Kouhai
Kouhai3y ago
Hmm, I think it should work from C# 8 Lett me test on Unity real quick It does compile
Xan.Nava
Xan.NavaOP3y ago
Weird, let me check on a clean project, maybe something else causing it to not work. Figured it out, the above does work(and corrected from before). I had a new before the call in the actual code which was causing the error.
Want results from more Discord servers?
Add your server