我有IBookRepository接口。该接口具有返回整数的方法CountAllBooks()。 如果我在方法内接收类型对象的值
public object Convert(object value)
如果传递的对象是IBookRepository类型,为什么我不能将它的CountAllBooks结果转换为整数,如
public object Convert(object value)
{
int c;
if(value is IBookRepository)
{
c = (int)value.CountAllBooks();
}
}
答案 0 :(得分:2)
public object Convert(object value)
{
int c;
if(value is IBookRepository)
{
c = (int)((IBookRepository)value).CountAllBooks();
}
return c;
}
答案 1 :(得分:2)
试试这个:
public object Convert(object value)
{
int c;
if(value is IBookRepository)
{
c = (int)(value as IBookRepository).CountAllBooks(); //casting "object" to "IBookRepository"
}
}