Skip to content

Instantly share code, notes, and snippets.

@Pan-Maciek
Created August 4, 2016 20:06
Show Gist options
  • Select an option

  • Save Pan-Maciek/5c02ed44df49a1c7d37b8f4f55322fef to your computer and use it in GitHub Desktop.

Select an option

Save Pan-Maciek/5c02ed44df49a1c7d37b8f4f55322fef to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1 {
public class Foo {
public override string ToString() {
return "Foo"; // dla testu zeby pokazało sie w konsoli to znaczy ze mamy dostęp do tej klasy z tablicy
}
}
public class Bar {}
public class Baz {}
public class Factory<TwojaKlasa> { // a w zasadzie jej typ
public Factory() {
for (int i = 0; i < 5; i++) {
// tu jest troszkę trki gdyż nie możnesz napisać poprostu new TwojaKlasa gdyż typ jest nie określony co nzaczy ze równie dobrze mogłeś tu wpisać np int
tab[i] = (TwojaKlasa)Activator.CreateInstance(typeof(TwojaKlasa), new object[] {});
}
}
public TwojaKlasa[] tab = new TwojaKlasa[5];
}
public class FooGenerator : Factory<Foo> {}
public class BarGenerator : Factory<Bar> {}
public class BazGenerator : Factory<Baz> {}
class Program {
static void Main(string[] args) {
Console.WriteLine(new FooGenerator().tab[0]);
Console.WriteLine(new BarGenerator().tab);
Console.WriteLine(new BazGenerator().tab);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment