'How do you access a common property of generic type inside a method in C# (using constraints?)
I have the same method that repeats in my project for different classes:
public Task DoSomething(Class1 class)
{
// do stuff
var commonProperty = class.commonProperty
// do stuff
}
And in a different service
public Task DoSomething(Class2 class)
{
// do stuff
var commonProperty = class.commonProperty
// do stuff
}
I have learnt that I can use generic types and believe this would be a good place to apply this - something like :
public Task DoSomething<T>(T class)
{
// do stuff
var commonProperty = class.commonProperty
// do stuff
}
But I can't access the property as such and can't find how to add multiple options for constraints.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
