WebApi + ClientApp, GraphQL, Reflection
This commit is contained in:
37
Reflection/LottoImplementation/Lotto.cs
Normal file
37
Reflection/LottoImplementation/Lotto.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using LottoInterfaces;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace LottoImplementation;
|
||||
|
||||
public class Lotto : ILotto
|
||||
{
|
||||
private int ballsToBeDrawn, numberOfBalls;
|
||||
public string? SomeProperty { get; set; }
|
||||
public Lotto(int ballsToBeDrawn, int numberOfBalls)
|
||||
{
|
||||
this.ballsToBeDrawn = ballsToBeDrawn;
|
||||
this.numberOfBalls = numberOfBalls;
|
||||
}
|
||||
|
||||
public List<int> DrawNumbers(bool sort)
|
||||
{
|
||||
List<int> list = new List<int>();
|
||||
Random r = new Random(DateTime.Now.Millisecond);
|
||||
int counter = ballsToBeDrawn;
|
||||
while (counter > 0)
|
||||
{
|
||||
int x = r.Next(1, numberOfBalls + 1);
|
||||
if (!list.Contains(x))
|
||||
{
|
||||
list.Add(x);
|
||||
counter--;
|
||||
}
|
||||
}
|
||||
if (sort)
|
||||
{
|
||||
list.Sort();
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
12
Reflection/LottoImplementation/LottoImplementation.csproj
Normal file
12
Reflection/LottoImplementation/LottoImplementation.csproj
Normal file
@@ -0,0 +1,12 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\LottoInterfaces\LottoInterfaces.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
8
Reflection/LottoInterfaces/ILotto.cs
Normal file
8
Reflection/LottoInterfaces/ILotto.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace LottoInterfaces;
|
||||
|
||||
public interface ILotto
|
||||
{
|
||||
List<int> DrawNumbers(bool sort);
|
||||
}
|
||||
8
Reflection/LottoInterfaces/LottoInterfaces.csproj
Normal file
8
Reflection/LottoInterfaces/LottoInterfaces.csproj
Normal file
@@ -0,0 +1,8 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
5
Reflection/Reflection.slnx
Normal file
5
Reflection/Reflection.slnx
Normal file
@@ -0,0 +1,5 @@
|
||||
<Solution>
|
||||
<Project Path="LottoImplementation/LottoImplementation.csproj" />
|
||||
<Project Path="LottoInterfaces/LottoInterfaces.csproj" />
|
||||
<Project Path="Reflection/Reflection.csproj" />
|
||||
</Solution>
|
||||
92
Reflection/Reflection/Program.cs
Normal file
92
Reflection/Reflection/Program.cs
Normal file
@@ -0,0 +1,92 @@
|
||||
using System.Diagnostics;
|
||||
using System.Reflection;
|
||||
using System.Runtime.Loader;
|
||||
using LottoInterfaces;
|
||||
|
||||
string assemblyLocation = @"../../../../LottoImplementation/bin/Debug/net10.0/LottoImplementation.dll";
|
||||
Console.WriteLine("Loading dll from file: " + Path.GetFullPath(assemblyLocation));
|
||||
AssemblyLoadContext loadctx = AssemblyLoadContext.Default;
|
||||
Assembly asm = loadctx.LoadFromAssemblyPath(Path.GetFullPath(assemblyLocation));
|
||||
Type? type = asm.GetType("LottoImplementation.Lotto");
|
||||
Debug.Assert(type != null, "LottoImplementation.Lotto not found!");
|
||||
PrintData(type);
|
||||
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("Creating an instance...");
|
||||
object? obj = Activator.CreateInstance(type, 7, 39);
|
||||
Debug.Assert(obj != null, "Could not create an instance of LottoImplementation.Lotto");
|
||||
|
||||
MethodInfo? info = type.GetMethod("DrawNumbers");
|
||||
Debug.Assert(info != null, "DrawNumbers method not found!");
|
||||
|
||||
object? result = info.Invoke(obj, new object[] { false });
|
||||
Debug.Assert(result != null && result is List<int>, "DrawNumbers returned null or not of type List<int>");
|
||||
|
||||
string print = string.Join(", ", (List<int>)result);
|
||||
Console.WriteLine(print);
|
||||
|
||||
//draw again, but this time sorted
|
||||
result = info.Invoke(obj, new object[] { true });
|
||||
print = string.Join(", ", (result as List<int>)!);
|
||||
Console.WriteLine(print);
|
||||
|
||||
//we know that the class implements ILotto
|
||||
ILotto loto = (ILotto)obj;
|
||||
List<int> numbers = loto.DrawNumbers(true);
|
||||
print = string.Join(", ", numbers);
|
||||
Console.WriteLine(print);
|
||||
|
||||
|
||||
static void PrintData(Type t)
|
||||
{
|
||||
ListVariousStats(t);
|
||||
ListFields(t);
|
||||
ListProps(t);
|
||||
ListMethods(t);
|
||||
ListInterfaces(t);
|
||||
}
|
||||
|
||||
static void ListVariousStats(Type t)
|
||||
{
|
||||
Console.WriteLine("***** Various Statistics *****");
|
||||
Console.WriteLine("Base class is: {0}", t.BaseType);
|
||||
Console.WriteLine("Is type abstract? {0}", t.IsAbstract);
|
||||
Console.WriteLine("Is type sealed? {0}", t.IsSealed);
|
||||
Console.WriteLine("Is type generic? {0}", t.IsGenericTypeDefinition);
|
||||
Console.WriteLine("Is type a class type? {0}", t.IsClass);
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
static void ListInterfaces(Type t)
|
||||
{
|
||||
Console.WriteLine("***** Interfaces *****");
|
||||
foreach (Type i in t.GetInterfaces())
|
||||
Console.WriteLine("->{0}", i.Name);
|
||||
}
|
||||
|
||||
static void ListProps(Type t)
|
||||
{
|
||||
Console.WriteLine("***** Properties *****");
|
||||
foreach (PropertyInfo info in t.GetProperties())
|
||||
Console.WriteLine("->{0}", info);
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
static void ListFields(Type t)
|
||||
{
|
||||
Console.WriteLine("***** Fields *****");
|
||||
//samo GetField() će vratiti samo javne varijable
|
||||
foreach (FieldInfo field in t.GetFields(BindingFlags.Instance | BindingFlags.NonPublic))
|
||||
Console.WriteLine("->{0}", field);
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
static void ListMethods(Type t)
|
||||
{
|
||||
Console.WriteLine("***** Methods *****");
|
||||
MethodInfo[] mi = t.GetMethods();
|
||||
foreach (MethodInfo m in mi)
|
||||
Console.WriteLine("->{0}", m);
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
14
Reflection/Reflection/Reflection.csproj
Normal file
14
Reflection/Reflection/Reflection.csproj
Normal file
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\LottoInterfaces\LottoInterfaces.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user