Files
Il2CppInspectorRedux/Il2CppTests/TestSources/CSharp2.cs
Robert Xiao 1c1f542107 Tests: Add new test assemblies for C# features (#36)
* Add new test assemblies for C# features

Three of these test assemblies go over several important features of C#,
organized by C# language version. PartialGenericTypes stresses closed
and partially closed generics. Finally, VTablesAndLayout tests the
layout of classes when translated to C++, and also contains code which
calls vtable and interface functions to test reverse engineering.

* Tests: Update .csproj

* Tests: Update .csproj

Co-authored-by: Katy Coe <djkaty@users.noreply.github.com>
2020-06-19 14:52:09 +02:00

101 lines
2.4 KiB
C#

using System;
/* C# 2.0 feature test */
namespace Il2CppTests.TestSources
{
public interface GenericInterface<T>
{
T func(T v);
T genericMethod<T2>(T v, T2 w);
}
public class GenericClass<T> : GenericInterface<T>
{
public T x;
public T func(T v) {
return v;
}
public T genericMethod<T2>(T v, T2 w) {
return x;
}
public static T myMethod(T x) {
return x;
}
public virtual bool returnBool() {
return true;
}
}
public class DerivedGenericClass<T, T2> : GenericClass<T>, GenericInterface<T2>
{
public T2 t2;
public T2 func(T2 v) {
return t2;
}
public T2 genericMethod<T3>(T2 v, T3 w) {
return v;
}
public T2 newGenericMethod() {
return t2;
}
public static int Return42() {
return 42;
}
}
public struct GenericStruct<T> : GenericInterface<T> where T : struct
{
public T a;
public T func(T v) {
throw new NotImplementedException();
}
public T genericMethod<T2>(T v, T2 w) {
throw new NotImplementedException();
}
public T? genericMethod2(T x) {
return (new GenericClass<T>()).returnBool() ? null : (T?)x;
}
public static GenericStruct<T> ReturnStruct(T y) {
var res = new GenericStruct<T>();
res.a = y;
return res;
}
}
public interface IVariance<in T1, out T2>
{
T2 func(T1 v);
}
public class UseGenerics
{
public static void Main(string[] args) {
GenericStruct<int>[] arr = new GenericStruct<int>[3];
arr[0] = GenericStruct<int>.ReturnStruct(3);
DerivedGenericClass<string, string> gc = new DerivedGenericClass<string, string>();
string c = gc.func("oops");
Console.WriteLine(c);
gc.genericMethod("hello", arr[0].genericMethod2(3) ?? 0);
GenericInterface<string> s = gc;
s.genericMethod("goodbye", c);
GenericClass<object>.myMethod(42);
IVariance<string, int> q = null;
q.func("nope");
foreach (var v in arr) {
v.genericMethod(64, "nope");
}
}
}
}