generics

How to test the type of a generic interface?

坚强是说给别人听的谎言 提交于 2021-02-06 13:01:44
问题 I'm not sure if the title makes sense, but I hope you can understand my question with some code. Given the following code for a publish/subscribe framework. type IMessage = interface ['{B1794F44-F6EE-4E7B-849A-995F05897E1C}'] end; ISubscriber = interface ['{D655967E-90C6-4613-92C5-1E5B53619EE0}'] end; ISubscriberOf<T: IMessage> = interface(ISubscriber) procedure Consume(const message: T); end; TMessageService = class private FSubscribers: TList<ISubscriber>; public constructor Create;

How to test the type of a generic interface?

两盒软妹~` 提交于 2021-02-06 13:00:48
问题 I'm not sure if the title makes sense, but I hope you can understand my question with some code. Given the following code for a publish/subscribe framework. type IMessage = interface ['{B1794F44-F6EE-4E7B-849A-995F05897E1C}'] end; ISubscriber = interface ['{D655967E-90C6-4613-92C5-1E5B53619EE0}'] end; ISubscriberOf<T: IMessage> = interface(ISubscriber) procedure Consume(const message: T); end; TMessageService = class private FSubscribers: TList<ISubscriber>; public constructor Create;

How to test the type of a generic interface?

帅比萌擦擦* 提交于 2021-02-06 12:58:08
问题 I'm not sure if the title makes sense, but I hope you can understand my question with some code. Given the following code for a publish/subscribe framework. type IMessage = interface ['{B1794F44-F6EE-4E7B-849A-995F05897E1C}'] end; ISubscriber = interface ['{D655967E-90C6-4613-92C5-1E5B53619EE0}'] end; ISubscriberOf<T: IMessage> = interface(ISubscriber) procedure Consume(const message: T); end; TMessageService = class private FSubscribers: TList<ISubscriber>; public constructor Create;

How to make method return the same generic as the input?

泪湿孤枕 提交于 2021-02-06 10:12:45
问题 I want to split a string delimited by commas and use the result as either a Seq or a Set : def splitByComma(commaDelimited: String): Array[String] = commaDelimited.trim.split(',') def splitByCommaAsSet(commaDelimited: String): Set[String] = splitByComma(commaDelimited).toSet def splitByCommaAsSeq(commaDelimited: String): Seq[String] = splitByComma(commaDelimited).toSeq val foods = "sushi,taco,burrito" val foodSet = splitByCommaAsSet(foods) // foodSet: scala.collection.immutable.Set[String] =

How to make method return the same generic as the input?

百般思念 提交于 2021-02-06 10:12:01
问题 I want to split a string delimited by commas and use the result as either a Seq or a Set : def splitByComma(commaDelimited: String): Array[String] = commaDelimited.trim.split(',') def splitByCommaAsSet(commaDelimited: String): Set[String] = splitByComma(commaDelimited).toSet def splitByCommaAsSeq(commaDelimited: String): Seq[String] = splitByComma(commaDelimited).toSeq val foods = "sushi,taco,burrito" val foodSet = splitByCommaAsSet(foods) // foodSet: scala.collection.immutable.Set[String] =

Why am I getting this exception when emitting classes that reference each other via value-type generics?

眉间皱痕 提交于 2021-02-06 08:43:59
问题 This code snippet is a simplified extract of my class-generation code, which creates two classes that reference each other as arguments in a generic type: namespace Sandbox { using System; using System.Reflection; using System.Reflection.Emit; internal class Program { private static void Main(string[] args) { var assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("Test"), AssemblyBuilderAccess.Run); var module = assembly.DefineDynamicModule("Test"); var typeOne = module

Why does an empty struct in C# consume memory

吃可爱长大的小学妹 提交于 2021-02-05 20:28:00
问题 I always understood structs (value types) contain exactly the number of bytes as defined in the fields of the structure... however, I did some tests and there seems to be an exception for the empty structs: public class EmptyStructTest { static void Main(string[] args) { FindMemoryLoad<FooStruct>((id) => new FooStruct()); FindMemoryLoad<Bar<FooStruct>>((id) => new Bar<FooStruct>(id)); FindMemoryLoad<Bar<int>>((id) => new Bar<int>(id)); FindMemoryLoad<int>((id) => id); Console.ReadLine(); }

Why does an empty struct in C# consume memory

允我心安 提交于 2021-02-05 20:25:22
问题 I always understood structs (value types) contain exactly the number of bytes as defined in the fields of the structure... however, I did some tests and there seems to be an exception for the empty structs: public class EmptyStructTest { static void Main(string[] args) { FindMemoryLoad<FooStruct>((id) => new FooStruct()); FindMemoryLoad<Bar<FooStruct>>((id) => new Bar<FooStruct>(id)); FindMemoryLoad<Bar<int>>((id) => new Bar<int>(id)); FindMemoryLoad<int>((id) => id); Console.ReadLine(); }

C# generic inheritance and covariance part 2

早过忘川 提交于 2021-02-05 12:37:24
问题 Here is my original thread: C# generic inheritance and covariance On just my read-only interfaces, I want inheritence to work. public delegate Boolean EnumerateItemsDelegate<out ItemType>(ItemType item); public interface IReadOnlyCollection<out ItemType> { Boolean ContainsItem(ItemType item); Array CopyToArray(); void EnumerateItems(EnumerateItemsDelegate<ItemType> enumerateDelegate); UInt32 Count { get; } UInt32 Capacity { get; } } Like this except where it compiles :-p This is what I would

C# generic inheritance and covariance part 2

元气小坏坏 提交于 2021-02-05 12:35:01
问题 Here is my original thread: C# generic inheritance and covariance On just my read-only interfaces, I want inheritence to work. public delegate Boolean EnumerateItemsDelegate<out ItemType>(ItemType item); public interface IReadOnlyCollection<out ItemType> { Boolean ContainsItem(ItemType item); Array CopyToArray(); void EnumerateItems(EnumerateItemsDelegate<ItemType> enumerateDelegate); UInt32 Count { get; } UInt32 Capacity { get; } } Like this except where it compiles :-p This is what I would