英文:
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<emailforEmployees> email, SecureEmail<EmailtoAdmins> tempEmail)
{
var emailtrail = new NotifyMe()
{
emailrequetid = email.Model.emailrequetid
}
}
I changed it to this:
private void AddInfo<T,U>(SecureEmail<T> email, SecureEmail<U> 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 '' accepting a first arugment 'T' could be found.
}
}
public class Email
{
public string from {get; set;}
}
public class SecureEmail<T> : Email
{
public T Model {get; set;}
}
sendEmail(SecureEmail<TypeofEmail> email)
{
var tempEmail = new SecureEmail<emailforEmployees>()
{
//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<emailforEmployees> email, SecureEmail<EmailtoAdmins>tempEmail)
{
var emailtrail = new NotifyMe()
{
emailrequetid = email.Model.emailrequetid
}
}
I changed it to this:
private void AddInfo<T,U>(SecureEmail<T> email, SecureEmail<U>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 '' accepting a first arugment 'T' could be found.
}
}
public class Email
{
public string from {get; set;}
}
public class SecureEmail<T> : Email
{
public T Model {get; set;}
}
sendEmail(SecureEmail<TypeofEmail> email)
{
var tempEmail = new SecureEmail<emailforEmployees>()
{
//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<T,U>(SecureEmail<T> email, SecureEmail<U>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<TypeofEmail> email)
{
var tempEmail = new SecureEmail<emailforEmployees>()
{
//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 '' accepting a first arugment 'T' could be found.
}
AddInfo(emailtrail)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论