Skip to main content
added 648 characters in body
Source Link
lukegravitt
  • 902
  • 4
  • 12

You need to use Generic constraints (and likely in this case) a second generic parameter as SLaks as indicated.

interface IRelated<TRelated> { bool isParent(TRelated parentObject); } class SomeClassParent {} class SomeClass : IRelated<SomeClassParent> { bool isParent(SomeClassParent parentObject) { // your logic here return true; } } class Thing<T, TParent> : ThingBase<T> where T : IRelated<TParent> {} 

To use:

var obj = new Thing<SomeClass, SomeClassParent>(); 

Here is another option. This one requires some runtime type checking (and allows isParent to be called with an object of any type), but it cleans up the syntax.

interface IRelated { bool isParent<T>(T parentObject); } class SomeClassParent {} class SomeClass : IRelated { bool isParent<T>(T parentObject) { if (parentObject is SomeClassParent) { // your logic here return true; } } } class Thing<T> : ThingBase<T> where T : IRelated {} 

To use:

var obj = new Thing<SomeClass>(); 

You need to use Generic constraints (and likely in this case) a second generic parameter as SLaks as indicated.

interface IRelated<TRelated> { bool isParent(TRelated parentObject); } class SomeClassParent {} class SomeClass : IRelated<SomeClassParent> { bool isParent(SomeClassParent parentObject) { // your logic here return true; } } class Thing<T, TParent> : ThingBase<T> where T : IRelated<TParent> {} 

To use:

var obj = new Thing<SomeClass, SomeClassParent>(); 

You need to use Generic constraints (and likely in this case) a second generic parameter as SLaks as indicated.

interface IRelated<TRelated> { bool isParent(TRelated parentObject); } class SomeClassParent {} class SomeClass : IRelated<SomeClassParent> { bool isParent(SomeClassParent parentObject) { // your logic here return true; } } class Thing<T, TParent> : ThingBase<T> where T : IRelated<TParent> {} 

To use:

var obj = new Thing<SomeClass, SomeClassParent>(); 

Here is another option. This one requires some runtime type checking (and allows isParent to be called with an object of any type), but it cleans up the syntax.

interface IRelated { bool isParent<T>(T parentObject); } class SomeClassParent {} class SomeClass : IRelated { bool isParent<T>(T parentObject) { if (parentObject is SomeClassParent) { // your logic here return true; } } } class Thing<T> : ThingBase<T> where T : IRelated {} 

To use:

var obj = new Thing<SomeClass>(); 
Source Link
lukegravitt
  • 902
  • 4
  • 12

You need to use Generic constraints (and likely in this case) a second generic parameter as SLaks as indicated.

interface IRelated<TRelated> { bool isParent(TRelated parentObject); } class SomeClassParent {} class SomeClass : IRelated<SomeClassParent> { bool isParent(SomeClassParent parentObject) { // your logic here return true; } } class Thing<T, TParent> : ThingBase<T> where T : IRelated<TParent> {} 

To use:

var obj = new Thing<SomeClass, SomeClassParent>();