Home > Enterprise >  C# ,Why I cannot access a class( Generic parameter) member in inherited class from a interface
C# ,Why I cannot access a class( Generic parameter) member in inherited class from a interface

Time:11-03

anyone can help me ?

I created one interface ,use a class as parameter, because I need to save different tables in sql database, so I want to create a method to save data to sql database.

public interface IDataAdapter {        
    void SaveExistingData<T>(T t);
    }

and I created another 4 class, like class BrandAdapter,class ProuductionAdapter ,class SupplyAdapter and OrderAdapter, those 4 class all inherited from interface IDataAdapter.

public class BrandAdapter : IDataAdapter {       
 public void SaveExistingData<Brand>(Brand updatedBrand) {
        string sql = "UPDATE tblBrands SET "   "brandName=@brandName "  
                     $"WHERE brandID={updatedBrand.brandId}"; //ERROR is at updatedBrand.brandId
        using (var connection = Helper.CreateDatabaseConnection()) {
            connection.Execute(sql, updatedBrand);
        }
    }

}

now when I access updatedBrand.brandId, IDE reminder me an error, I cannot access all member in Brand class, and "Brand" is dark yellow color, I can not access its definition by click F12, it looks like the system didnot reconize it.

Brand class, BrandAdapter class and IDataAdapter interface ,they are at one project.

if I only create Brand , its ok, I can use all members of Brand class, but When I use generic, its not ok. Because I want to use other class as a parameter, so I tried to use generic.

Thanks in advace.

CodePudding user response:

How about this ?

public class Brand
{
    public int brandId { get; set; }
}

public interface IDataAdapter<T>
{
    void SaveExistingData(T t);
}

public class BrandAdapter : IDataAdapter<Brand>
{
    public void SaveExistingData(Brand updatedBrand) 
    {
        int abcd = updatedBrand.brandId;
    }
}
  •  Tags:  
  • c#
  • Related