为什么我的接口在使用的时候总是Null

SiriusGuo 2014-01-23 10:48:11

class Program
{
static ITest i { get; set; }
static void Main(string[] args)
{
i.SayHi("GT");//这里的i总是Null啊
Console.ReadKey();
}
}

public interface ITest
{
void SayHi(string str);
}
class Test : ITest
{
public void SayHi(string str)
{
Console.Write("Hi," + str);
}
}

...全文
269 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
SiriusGuo 2014-01-24
  • 打赏
  • 举报
回复
引用 5 楼 wyd1520 的回复:
[Inject] 用到了这个,可能有用到返射,你看不到吧。。。实例 public IRolesRepository RolesRepository { get; set; } 这个在其他地方有被赋值
依赖注入没找到这个的Bind,奇怪了,没发现原因。换了个地方声明莫名其妙的好了。
本拉灯 2014-01-23
  • 打赏
  • 举报
回复
[Inject] 用到了这个,可能有用到返射,你看不到吧。。。实例 public IRolesRepository RolesRepository { get; set; } 这个在其他地方有被赋值
SiriusGuo 2014-01-23
  • 打赏
  • 举报
回复
引用 2 楼 u011130289 的回复:
没有实例化,当然为空 Test i = new Test(); i.SayHi("GT"); Console.ReadKey();
帮忙看下楼上的代码为啥能跑呢?
SiriusGuo 2014-01-23
  • 打赏
  • 举报
回复
引用 1 楼 wyd1520 的回复:
static ITest i { get; set; } 没给他赋值呀 static void Main(string[] args) { i=new Test(); i.SayHi("GT");//这里的i总是Null啊 Console.ReadKey(); }
那为啥这个好用呢?疑惑啊,我也记得要new一个实现它的类啊

    public class RolesServiceImp:IRolesService
    {
        [Inject]
        public IUnitOfWork UnitOfWork { get; set; }

        [Inject]
        public IRolesRepository RolesRepository { get; set; }

        [Inject]
        public IUserProfileRepository UserProfileRepository { get; set; }

        [Inject]
        public IPermissionsRepository PermissionsRepository { get; set; }
        public IEnumerable<webpages_Roles> GetRolesList()
        {
            var query = RolesRepository.GetAll(); //这两个方法都用到了接口呢,也没new
            return query;
        }

        public IEnumerable<webpages_Roles> Query(Expression<Func<webpages_Roles, bool>> where)
        {
            return RolesRepository.GetMany(where);//这两个方法都用到了接口呢,也没new

        }
Regan-lin 2014-01-23
  • 打赏
  • 举报
回复
没有实例化,当然为空 Test i = new Test(); i.SayHi("GT"); Console.ReadKey();
本拉灯 2014-01-23
  • 打赏
  • 举报
回复
static ITest i { get; set; } 没给他赋值呀 static void Main(string[] args) { i=new Test(); i.SayHi("GT");//这里的i总是Null啊 Console.ReadKey(); }

111,094

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧