I'm having trouble retrieving an object to get a specific attribute.
I want to override the Equals
method in my class, to compare if the title of the objects are identical. In this case, it does not matter if the type and the other attributes are different. I just want to compare if the Title
attribute of both are equal.
My code looks like this:
namespace Kitty.Core.Blocks
{
class Block<T>
{
public string Title { get; private set; }
public T Contents { get; private set; }
public Block(string title, T contents)
{
this.Title = title;
this.Contents = contents;
}
public override bool Equals(object obj)
{
Block<?> other = obj; // ?
return string.Equals(this.Title, other.Title);
}
}
}
The problem is this:
public override bool Equals(object obj)
{
Block<?> other = obj; // Como recupero "obj"?
return string.Equals(this.Title, other.Title);
}
I tried this way:
Block<?> other = (Block<?>) obj;
And so:
Block other = (Block) obj;
But I'm forced to specify the type:
Using the generic type '
Block<T>
' requires 1 type arguments.
As I'm overwriting, it's no use changing the method's signature and changing object
to the same class name.
So, how can I retrieve this obj
, keeping its generic type (which I do not know what it will be) to compare only the Title
attribute of both?