7

i want to create an anonymous type inside a function, when the anonymous type properties are the function parameters.

for example, for the function: private bool CreatePerson(string FirstName, string LasName, int Age, int height);

i will have an anonymous type with the properties: FirstName,LasName,Age and height. and the values of the function parameters will be the values of the anonymous type properties.

private bool CreatePerson(string FirstName, string LasName, int Age, int height)
    {
        // Get this method parameters
        MethodBase currentMethod =  MethodBase.GetCurrentMethod();
        ParameterInfo[] parametersInfo = currentMethod.GetParameters();

        // create an object of the parameters from the function.
        foreach (ParameterInfo paramInfo in parametersInfo)
        {
            // add a property with the name of the parameter to an anonymous object and insert its value to the property.
            // WHAT DO I DO HERE?
            ....
        }

        return true;
    }
Rodniko
  • 4,926
  • 20
  • 69
  • 93
  • You want to create an anonymous type with known field names and types, why do you need reflection? new { FirstName = FirstName, ... } would do just fine? – SoftMemes Oct 24 '10 at 13:06
  • He wants that to be defined in runtime - I believe. – Aliostad Oct 24 '10 at 13:10
  • @Aliostad - but the description just states that the _values_ should come from the parameters, the names of the parameters/properties are there already ... – SoftMemes Oct 24 '10 at 13:12
  • Why are you trying to do this? It's hard to do this because it's probably a bad idea. – siride Dec 27 '14 at 18:53

2 Answers2

3

If I understood correctly and you want to define the properties at runtime, this is not possible. Although in anonymous types you may be able to create types that you define there and then by assigning values, the name of the properties must be known at compile time.

In fact, the type is anonymous to you but not to the CLR. If you look at the assembly in ildasm.exe or reflector, you will see these anonymous types with strange names always having <> in their names.

C#'s dynamic might be able to help here but as far as I know, they help with communicating with objects that are that we do not have type information for, not creating - but there might be a way that I do not know.

Aliostad
  • 80,612
  • 21
  • 160
  • 208
  • It would be quite possible to use Reflection.Emit to generate a new type at runtime though, but I can't see why you would want to for something like this. – SoftMemes Oct 24 '10 at 13:09
  • True, writing code and compiling is always a possibility but that is not he wants to do. – Aliostad Oct 24 '10 at 13:12
  • Reflection.Emit is not really "writing code and compiling" as there's no source code, but I agree that's most likely not what he needs. – SoftMemes Oct 24 '10 at 13:20
  • You can use dynamic and ExpandoObject from C# 4.0, but (unless you use Reflection.Emit) you still have to explicitly operate on the parameters passed to the method, which is not what @Rodniko wanted I believe. So generally I agree - there's no way to do it without a lot of unnecessary "magic". – NOtherDev Oct 24 '10 at 13:27
  • thanks guys for all your help, by the way , the object doesn't have to have properties , i can settle with public String variables only. so you are saying there is no way to craete my own objet from the reflection PropertyInfo array? 'A' - are you saying there is a way to do that with Dynamic and ExpandoObject? what do you mean by "still have to explicitly operate on the parameters passed to the method"? – Rodniko Oct 25 '10 at 07:53
0

Could you not use the "Linq to DataSet" Field<T>(String Name) design pattern? In fact why not just use a DataTable. The compiler does not need to know that the field exists, only what type it is to be type safe. One reason to do this would be to implement some type of parser to generate filters, or to configure field names dynamically.