小编Phi*_*hil的帖子

如何将泛型类型参数传递给从构造函数调用的方法?

这是构造函数:

 public PartyRoleRelationship(PartyRole firstRole, PartyRole secondRole)
        {
            if (firstRole == secondRole)
                throw new Exception("PartyRoleRelationship cannot relate a single role to itself.");

            if (firstRole.OccupiedBy == null || secondRole.OccupiedBy == null)
                throw new Exception("One or both of the PartyRole parameters is not occupied by a party.");

            // Connect this relationship with the two roles.
            _FirstRole = firstRole;
            _SecondRole = secondRole;


            T = _FirstRole.GetType().MakeGenericType();

            _SecondRole.ProvisionRelationship<T>(_FirstRole); // Connect second role to this relationship.
        }
Run Code Online (Sandbox Code Playgroud)

在最后一行,它在_SecondRole上调用ProvisionRelationship,它给了我运行时错误:无法找到类型或命名空间'T'...

我如何(a)正确分配T,或(b)通过构造函数传递泛型?我一直在查看相当多的帖子,但可能由于缺乏理解而遗漏了一些内容.任何人对此的帮助将不胜感激.

c# generics constructor

7
推荐指数
1
解决办法
6万
查看次数

标签 统计

c# ×1

constructor ×1

generics ×1