0

I've created a simple class that is a descendant of DynamicObject:

public class DynamicCsv : DynamicObject
{

    private Dictionary<string, int> _fieldIndex;
    private string[] _RowValues;

    internal DynamicCsv(string[] values, Dictionary<string, int> fieldIndex)
    {
        _RowValues = values;
        _fieldIndex = fieldIndex;
    }

    internal DynamicCsv(string currentRow, Dictionary<string, int> fieldIndex)
    {
        _RowValues = currentRow.Split(',');
        _fieldIndex = fieldIndex;
    }

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        result = null;
        dynamic fieldName = binder.Name.ToUpperInvariant();
        if (_fieldIndex.ContainsKey(fieldName))
        {
            result = _RowValues[_fieldIndex[fieldName]];
            return true;
        }
        return false;
    }

    public override bool TrySetMember(SetMemberBinder binder, object value)
    {
        dynamic fieldName = binder.Name.ToUpperInvariant();
        if (_fieldIndex.ContainsKey(fieldName))
        {
            _RowValues[_fieldIndex[fieldName]] = value.ToString();
            return true;
        }
        return false;
    }

}

I use the descendant object by doing the following:

    protected string[] _currentLine;
    protected Dictionary<string, int> _fieldNames;
...
                _fieldNames = new Dictionary<string, int>();
...
                _CurrentRow = new DynamicCsv(_currentLine, _fieldNames);

When I try to use the _CurrentRow with dot notation:

int x = _CurrentRow.PersonId;

I get the following error message:

"'object' does not contain a definition for 'property' and no extension method 'property' accepting a first argument of type 'object' could be found"

I can resolve the property in the immediate window using VB without any issues though:

? _CurrentRow.PersonId
BermudaLamb
  • 273
  • 3
  • 5
  • 24
  • Maybe it's just me.. but I just can't see where you've declared `PersonId`.. is it a property? Public variable? .. where it is? How are you declaring `_CurrentRow`? – Simon Whitehead Apr 02 '13 at 22:26

2 Answers2

4

it looks like _CurrentRow is typed to object but that you want dynamic lookup to occur on it. If that's the case then you need to change the type to dynamic

dynamic _CurrentRow;
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • Yes, it looks to me like you're creating it as an "object" type. Note that in C#, polymorphism is explicit, so even if you're defining a DynamicCsv, if the given type is "object", then it will only be allowed to consider the fields, methods and properties in "object" without casting. As has been said, you need to name it as dynamic, cast it to the correct type before calling PersonId, or better still, use a static type for it. In general, if you can avoid using dynamic typing, do, as it can cause typing issues if you're not careful. – Hoeloe Apr 02 '13 at 22:28
  • That was it! One little word made all of the difference, thanks. – BermudaLamb Apr 03 '13 at 16:10
1

You're not showing the declaration of _CurrentRow. It should be declared as

dynamic _CurrentRow in order to have the dynamic behavior.

Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154