我正在尝试做以下事情:
GetString(
inputString,
ref Client.WorkPhone)
private void GetString(string inValue, ref string outValue)
{
if (!string.IsNullOrEmpty(inValue))
{
outValue = inValue;
}
}
Run Code Online (Sandbox Code Playgroud)
这给了我一个编译错误.我认为我很清楚我想要实现的目标.基本上我想GetString
将输入字符串的内容复制到WorkPhone
属性Client
.
是否可以通过引用传递属性?
我有一个对象,我想以这种方式构造:
var foo = new FancyObject(customer, c=>c.Email); //customer has Email property
Run Code Online (Sandbox Code Playgroud)
我该如何申报第二个参数?
访问所选属性setter/getter的代码如何?
UPD.模型中有多个实体具有Email属性.所以签名可能看起来像:
public FancyObject(Entity holder, Expression<Func<T>> selector)
Run Code Online (Sandbox Code Playgroud)
和构造函数调用
var foo = new FancyObject(customer, ()=>customer.Email);
Run Code Online (Sandbox Code Playgroud) 我需要使用模式工厂的想法将我的Person类实体中的实体属性Address与我的FactoryEntities类中的表达式linq相关联,看看这就是我拥有的和我想做的事情:
Address address = new Address();
address.Country = "Chile";
address.City = "Santiago";
address.ZipCode = "43532";
//Factory instance creation object
//This is idea
Person person = new FactoryEntity<Person>().AssociateWithEntity(p=>p.Address, address);
public class Person: Entity
{
public string Name{ get; set; }
public string LastName{ get; set; }
public Address Address{ get; set; }
}
public class Address: Entity
{
public string Country{ get; set; }
public string City{ get; set; }
public string ZipCode{ get; set; }
}
public class FactoryEntity<TEntity> where …
Run Code Online (Sandbox Code Playgroud) 我的问题与以下两个问题非常相似,但是我有一个附加要求,即这些问题不能满足。
就像那些问题一样,我有一个Expression<Func<TEntity, TProperty>>
要在其中为指定属性设置值的地方。如果表达式的主体仅深一层,这些解决方案将非常有用,例如,x => x.FirstName
但是如果该主体更深,它们将根本无法工作x => x.Parent.FirstName
。
有什么方法可以采用这种更深层的表达式并将其值设置为?我不需要极其强大的执行延迟解决方案,但是我确实需要可以在对象上执行的操作,无论是1层还是多层,它都能正常工作。我还需要支持你期望在一个数据库中最典型的类型(long
,int?
,string
,Decimal
,DateTime?
,等。虽然我不在乎像地理类型有关更复杂的事情)。
为了进行对话,我们假设我们正在处理这些对象,尽管假设我们需要深度处理N个级别,而不仅仅是1或2:
public class Parent
{
public string FirstName { get; set; }
}
public class Child
{
public Child()
{
Mom = new Parent(); // so we don't have to worry about nulls
}
public string FavoriteToy { get; set; }
public Parent Mom { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
假设这是我们的单元测试: …