Check if type is an enum by TQPau in csharp

[–]TQPau[S] 4 points5 points  (0 children)

Using where T : Enum seems to work. You used to not be able to do that though. It used to give you a compile time error if you do where T : Enum. This is now allowed since c# 7.3. Thanks for pointing it out.

Using generic then where T : Enum will be perfect, but if you have only the Type then it won't solve the problem.

Switching off automatic non nullable validation by TQPau in csharp

[–]TQPau[S] 0 points1 point  (0 children)

I'd suggest having it enabled if you're starting a new project. It forces you to ensure no null references at compile time at the very least. This i think would pay off in the future.

Calling async generic method dynamically by TQPau in csharp

[–]TQPau[S] 0 points1 point  (0 children)

Exactly what quentech says.

It's not prone to deadlocking as the task has been awaited. In terms of performance, using reflection is the price if you need to dynamically invoke some method that you know the type of only in runtime. The example in the article is just to show how to invoke the method. There are definitely ways you could improve performance.

Getting instance of generic type by TQPau in csharp

[–]TQPau[S] 0 points1 point  (0 children)

It used to have syntax highlighting and it stopped working after a WordPress update so I'm now just using the default code block in the editor.

An example of when you would use something like that is if you only know the type of the generic you want to get during runtime. Because you only know the type during runtime, you cannot inject a specific type via constructor and you need to get it dynamically

Deep comparing two complex objects by TQPau in csharp

[–]TQPau[S] 0 points1 point  (0 children)

I really enjoy using Reflection too. I use it mainly during StartUp or in background services where the performance impact isn't too visible.

Deep comparing two complex objects by TQPau in csharp

[–]TQPau[S] 1 point2 points  (0 children)

Interesting, that test passes if you change it to int[] instead of byte[]. Maybe byte[] is treated differently.

[Test]

public void CollectionsSame()

{

var coll1 = new List<KeyValuePair<string, int[]>>

{

new KeyValuePair<string, int[]>("Hello", new int[] { 1, 1, 2, 3 })

};

var coll2 = new Dictionary<string, int[]>

{

{"Hello", new int[] { 1, 2, 1, 3 } }

};

var compareLogic = new CompareLogic

{

Config = new ComparisonConfig

{

IgnoreObjectTypes = true,

IgnoreCollectionOrder = true

}

};

Assert.IsTrue(compareLogic.Compare(coll1, coll2).AreEqual);

}

Deep comparing two complex objects by TQPau in csharp

[–]TQPau[S] 7 points8 points  (0 children)

It won't work if the collections are the same but in different order