braks
braks

Reputation: 1610

Interface does not contain a definition for property

Firstly, I am not a very experienced .net developer, so please forgive me if this question is insane.

I have created a bunch of models which implement my interface IEntity. All I want the interface to do (for now) is to ensure those models contain a Created property to store the date.

I would like to neglect adding a Created value to each instance, and let some common code handle that, which is where I'm encountering the problem.

The following code produces a build error:

public class Service<IEntity>
{
    // other code omitted 
    public virtual void Insert(IEntity entity)
    {
        entity.Created = DateTime.Now;

        // other code omitted
    }
}

Error 15 'IEntity' does not contain a definition for 'Created' and no extension method 'Created' accepting a first argument of type 'IEntity' could be found (are you missing a using directive or an assembly reference?)

But here is my definition of IEntity, and of course implementations of IEntity will always have a Created property.

public interface IEntity
{
    DateTime Created { get; set; }
}

So is there a way to do what I want to do, or should I just forget about trying to handle Created in this way?

Upvotes: 1

Views: 3480

Answers (3)

braks
braks

Reputation: 1610

The solution I found to be suitable was to use Constraints. So the code became this:

public class Service<TEntity> where TEntity : IEntity
{
    // other code omitted 
    public virtual void Insert(TEntity entity)
    {
        entity.Created = DateTime.Now;

        // other code omitted
    }
}

https://msdn.microsoft.com/en-us/library/d5x73970.aspx

This allows me to use this class with any of the "bunch of models which implement my interface IEntity" I mentioned.

Upvotes: 1

bgs264
bgs264

Reputation: 4642

If you place the cursor within the IEntity parameter type of the Insert method and press F12, it'll go to the definition of IEntity.

Make sure it's the one you pasted, and you don't have a different IEntity in a different namespace somewhere which has caused the confusion.

I've mocked up your example and it works fine for me.

The below compiles and the test passes.

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

namespace Tests
{
    public interface IEntity
    {
        DateTime Created { get; set; }
    }

    public class MyClass : IEntity
    {
        public DateTime Created { get; set; }
    }

    [TestClass]
    public class UnitTest1
    {
        private readonly DateTime _exampleDate;

        public UnitTest1()
        {
            _exampleDate = DateTime.Now;
        }

        public virtual void Insert(IEntity entity)
        {
            entity.Created = _exampleDate;
            // other code ommitted
        }

        [TestMethod]
        public void TestMethod1()
        {
            MyClass myTest = new MyClass();
            Insert(myTest);

            Assert.AreEqual(_exampleDate, myTest.Created);
        }
    }
}

Upvotes: 1

pinki
pinki

Reputation: 970

Are you sure, you are not using another IEntity in the Method then yours? Check your using statements. There also seems to be a naming conflict with "Created" as it is colored strangely.

Upvotes: 0

Related Questions