I'm looking for a way to pass in a list of strongly typed property names into a method that I can then dissect and get the properties that the caller is interested in. The reason I want to do this is to have a copy method that only copies the fields the user specifies. Right now, the method takes a list of strings to use with the Getvalues and get properties methods in reflection, but I want to guard against refactoring of properties and the strings not being updated by the developer.
I found this article Here, but unfortunately, it does not do a list. I can do something like:
public static void Copy(Expression<Func<TObject>> propertiesToCopy )
{
}
And then have the caller do
PropertyCopier<List<string>>.Copy(() => data);
But then I have to specify how many properties the caller can have like this:
public static void Copy(Expression<Func<TObject>> propertiesToCopy,Expression<Func<TObject>> propertiesToCopy2, Expression<Func<TObject>> propertiesToCopy3 )
{
}
This would allow for three properties. Is there anyway to add it to a List or Queryable<> to allow as many properties as the caller wants? I tried using the Add in List and having the Expression
Thanks in advance
Edit: I did find a few articles this evening that refer to using the C# param keyword to accomplish this. Are there any better or more efficient ways, or is this the best way to do it?
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
Use the params keyword to define a method that takes a variable number of arguments:
public static void PrintPropertyNames<T>(params Expression<Func<T, object>>[] properties)
{
foreach (var p in properties)
{
var expression = (MemberExpression)((UnaryExpression)p.Body).Operand;
string memberName = expression.Member.Name;
Console.WriteLine(memberName);
}
}
For instance, you could call the PrintPropertyNames method passing two expressions:
PrintPropertyNames<FileInfo>(f => f.Attributes, f => f.CreationTime);
This example displays the following output to the console:
Attributes CreationTime
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With