2

Lets say I have the following class:

public class Provider
{
    ...
    public sealed class Slice
    {
         public readonly double firstName;
         public readonly double secondName;
         public readonly double thirdName;
         ...
    }
    ...
}

This class is used to hold a sliding time series and the contained Slice class is the return value. (Provider.Last property returns the latest instance of Slice).

I need to get the value of the properties of that latest returned Slice class by name of the property.

PropertyInfo secondNameProperty = Provider.Last.GetType().GetProperty("secondName");
double secondNameValue = (double)secondNameProperty.GetValue(Provider.Last, null);

GetProperty returns null. How can I do this?

3 Answers 3

9

Look at your Slice class:

public sealed class Slice
{
     public readonly double firstName;
     public readonly double secondName;
     public readonly double thirdName;
     ...
}

Those aren't properties. They're fields. Either make them properties, or use Type.GetField() instead. Using properties would generally be a better idea, IMO, and needn't be hard. For example, if you just wanted to make them publicly read-only, you could use:

public sealed class Slice
{
     public double FirstName { get; private set; }
     public double SecondName { get; private set; }
     public double ThirdName { get; private set; }
     ...
}

Alternatively you could declare read-only fields directly, and then expose them via properties. It's a bit more work than using automatically implemented properties, but it removes the potential for setting the property within Slice itself.

(As an aside, do you really have a firstName field of type double? Odd.)

Sign up to request clarification or add additional context in comments.

1 Comment

And then name them as properties too: FirstName
0

As Jon Skeet says, these aren't properties. Readonly properties would look like this

public sealed class Slice
{
    public double FirstName { get; private set }
    public double SecondName  { get; private set }
    public double ThirdName { get; private set }
    ...
}

or

public sealed class Slice
{
    private double _firstName;
    public double FirstName { get { return _fristName; } }

    private double _secondName;
    public double SecondName { get { return _secondName; } }

    private double _thirdName;
    public double ThirdName { get { return _thirdName; } }

    ...
}

Comments

0

You can use GetMember() - Members include properties, methods, fields, events, and so on. Or use Jon Skeet's answer.

Note, the if you use GetField() and later change your fields to properties, GetMember() will continue to find the Field or Property in question without refactoring the code, whereas GetField() will return null.

http://msdn.microsoft.com/en-us/library/xdtchs6z.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.