Home > Net >  Dynamic splicing Expression
Dynamic splicing Expression

Time:11-18

 

The class Person
{
Public int Id {get; The set; }

Public string Name {get; The set; }

Public string Address {get; The set; }

}

Class Program
{
The static void Main (string [] args)
{

Expression Expression=exp=& gt; New {exp. Id};
If conditions (1)
{
Expression=exp=& gt; New {exp. Name};
}

If conditions (2)
{
Expression=exp=& gt; New {exp. Address};
}

Expression=exp=& gt; New {exp. Id, exp. Name, exp. Address};

Console. WriteLine (" Hello World!" );
The Console. ReadLine ();
}


}



Dynamic splicing Expression
Finally realizes the effect:
Expression=exp=& gt; New {exp. Id, exp. Name, exp. Address};

CodePudding user response:

To study the Expression tree Expression, certainly can solve your problem, but I think the meaning is not big, I'm in the project practice, did not come across with this need, or rather, such requirements are too weak, you don't is the removal of one or two more columns, and no big deal,
To this, you might have to write linq is more complex, might have lost the strong type (for example, you might end up with a string to express column, no difference between that and write SQL, degradation), you have to face you want to take the column from the different multi-table join, implementation may be tedious,

So there's no need, now that you have is an object-oriented programming, you get this object, you have to accept the all attributes of the object, can't say that you get the object and then said, you remove the I what what attributes, this also can't do,
It is good to accept,

CodePudding user response:

Learn, play a thing to launch a best-selling products, often within 10 years of experience, often between hundreds of discretion,

CodePudding user response:

This expression can do
New {exp. Id, exp. Name, exp. Address};
This is the c # compiler grammar sugar,
Equivalent to a c # defines a class
//class XXX will actually is a strange name
{
Public int id {get; The set; }
Public string name {get; The set; }
Public string address {get; The set; }
}
The
New {exp. Id, exp. Name, exp. Address};
In fact is
New XXX {id=exp. Id, name=exp. Name, address=exp. Address};

And expression is a runtime execution, it could not create type,

CodePudding user response:

Tell me about your first real demand,
  •  Tags:  
  • C#
  • Related