通用方法参数与通用属性引发 T 不包含定义的错误

huangapple go评论136阅读模式
英文:

Generic method parameter with generic property throws T does not contain a definition error

问题

I'm trying to abstract a method that currently takes in 2 parameters. Both parameters are of Class SecureEmail<T>. Initially the types were specified, i want to make it generic now but i'm getting an error when assigning them, on the parameter that is also generic. I have added some code to explain below.

Initially AddInfo looked like this (Worked fine):

private void AddInfo(SecureEmail&lt;emailforEmployees&gt; email, SecureEmail&lt;EmailtoAdmins&gt; tempEmail)
{
  var emailtrail = new NotifyMe()
     {
	    emailrequetid = email.Model.emailrequetid
     }	 
}

I changed it to this:

private void AddInfo&lt;T,U&gt;(SecureEmail&lt;T&gt; email, SecureEmail&lt;U&gt; tempEmail)
{
  var emailtrail = new NotifyMe()
     {
	    emailrequetid = email.Model.emailrequetid //error on emailrequetid 
            // T does not contain a definition for emailrequstid and no accessible extension method &#39;&#39; accepting a first arugment &#39;T&#39; could be found.
     }	 
}

public class Email
{
 public string from {get; set;}
}

public class SecureEmail&lt;T&gt; : Email
{
 public T Model {get; set;}
}

sendEmail(SecureEmail&lt;TypeofEmail&gt; email)
{
    var tempEmail = new SecureEmail&lt;emailforEmployees&gt;()
	{
	    //populate
	}
     
  tempEmail.Model = new EmailtoAdmins()
  {
     /////Popuilate Model
  }
  
  AddInfo(email, tempEmail)
}

I'm expecting to be able to call AddInfo and specify any type i want for T.

英文:

I'm trying to abstract a method that currently takes in 2 parameters. Both parameters are of Class SecureEmail<T>. Initially the types were specified, i want to make it generic now but i'm getting an error when assigning them, on the parameter that is also generic. I have added some code to explain below.

Initially AddInfo looked like this (Worked fine):

private void AddInfo(SecureEmail&lt;emailforEmployees&gt; email, SecureEmail&lt;EmailtoAdmins&gt;tempEmail)
{
  var emailtrail = new NotifyMe()
     {
	    emailrequetid = email.Model.emailrequetid
     }	 
}

I changed it to this:

private void AddInfo&lt;T,U&gt;(SecureEmail&lt;T&gt; email, SecureEmail&lt;U&gt;tempEmail)
{
  var emailtrail = new NotifyMe()
     {
	    emailrequetid = email.Model.emailrequetid //error on emailrequetid 
            // T does not contain a definition for emailrequstid and no accessible extension method &#39;&#39; accepting a first arugment &#39;T&#39; could be found.
     }	 
}

public class Email
{
 public string from {get; set;}
}

public class SecureEmail&lt;T&gt; : Email
{
 public T Model {get; set;}
}

sendEmail(SecureEmail&lt;TypeofEmail&gt; email)
{
    var tempEmail = new SecureEmail&lt;emailforEmployees&gt;()
	{
	    //populate
	}
     
  tempEmail.Model = new EmailtoAdmins()
  {
     /////Popuilate Model
  }
  
  AddInfo(email, tempEmail)
}

I'm expecting to be able to call AddInfo and specify any type i want for T.

答案1

得分: 2

你需要使用constraints 来确保 emailrequetid 被定义。例如,你可以定义以下接口:

public interface ISpecific
{
    public string EmailRequestId { get; set; }
}

然后使用它来指定 Model 具有属性 EmailRequestId

private void AddInfo<T, U>(SecureEmail<T> email, SecureEmail<U> tempEmail)
    where T : ISpecific
{
    var emailTrail = new NotifyMe()
    {
        EmailRequestId = email.Model.EmailRequestId
    };
    // 做一些其他操作
}
英文:

You need to use constraints to make sure, the emailrequetid is defined. For example, you could define following interface:

public interface ISpecific
{
    public string EmailRequestId { get; set; }
}

Then use it to specify the Model has property EmailRequestId:

private void AddInfo&lt;T,U&gt;(SecureEmail&lt;T&gt; email, SecureEmail&lt;U&gt;tempEmail)
    where T : ISpecific
{
    var emailTrail = new NotifyMe()
    {
        EmailRequestId = email.Model.EmailRequestId
    };
    // do something else
}

答案2

得分: 0

以下是翻译好的代码部分:

而不是尝试使用泛型,最好删除依赖项。我测试过了,更加清晰:

private void AddInfo(NotifyMe emailtrail)
{
  Dosomething(emailtrail);
}

sendEmail(SecureEmail<邮件类型> email)
{
    var tempEmail = new SecureEmail<员工邮件>()
    {
        //填充
    }

  tempEmail.Model = new 发送给管理员的邮件()
  {
     //填充模型
  }
  
//从AddInfo中移动到这里
var emailtrail = new 通知我()
         {
            emailrequetid = email.Model.emailrequetid //emailrequetid上的错误
                // T不包含emailrequstid的定义,也找不到可接受第一个参数为'T'的可访问扩展方法''。
         }   

  AddInfo(emailtrail)
}
英文:

Instead of trying to use generics, its better to remove the dependency. I tested this and it cleaner:

private void AddInfo(NotifyMe emailtrail)
{
  Dosomething(emailtrail);
}

sendEmail(SecureEmail&lt;TypeofEmail&gt; email)
{
    var tempEmail = new SecureEmail&lt;emailforEmployees&gt;()
    {
        //populate
    }
     
  tempEmail.Model = new EmailtoAdmins()
  {
     /////Popuilate Model
  }
  
//move this here from Addinfo
var emailtrail = new NotifyMe()
         {
            emailrequetid = email.Model.emailrequetid //error on emailrequetid 
                // T does not contain a definition for emailrequstid and no accessible extension method &#39;&#39; accepting a first arugment &#39;T&#39; could be found.
         }   

  AddInfo(emailtrail)
}

huangapple
  • 本文由 发表于 2023年3月12日 10:00:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75710681.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定