re: Write a function that shows off something unique or interesting about the language you're using VIEW POST

FULL DISCUSSION
 
        /// <summary>
        /// The variable value.
        /// </summary>
        public object Value
        {
            get
            {
                if (parentModel != null)
                {
                    var mi = parentModel.Type.GetMember(Name);

                    if (mi.Length > 0)
                    {
                        if (mi[0] is FieldInfo)
                        {
                            value = ((FieldInfo)mi[0]).GetValue(parentObject);
                            return value;
                        }
                        else if (mi[0] is PropertyInfo)
                        {
                            if (parentModel.Type is System.Collections.IEnumerable)
                            {
                                value = ((PropertyInfo)mi[0]).GetValue(parentObject, new object[] { index });
                                return value;
                            }
                            else
                            {
                                value = ((PropertyInfo)mi[0]).GetValue(parentObject);
                                if (parentModel.PropertyChanged != null)
                                    parentModel.PropertyChanged(this, new PropertyChangedEventArgs("Members"));
                                return value;
                            }
                        }
                        else
                            return value;
                    }
                    else
                        return value;
                }
                else
                    return value;
            }
            set
            {
                if (parentModel != null)
                {
                    var mi = parentModel.Type.GetMember(Name);

                    try
                    {
                        if (mi != null && mi.Length > 0)
                        {
                            bool didSet = false;
                            if (value is string)
                            {
                                TypeConverter tc = TypeDescriptor.GetConverter(Type);
                                this.value = tc.ConvertFromString((string)value);
                            }

                            if (mi[0] is FieldInfo)
                            {
                                var fInfo = mi[0] as FieldInfo;
                                var val = fInfo.GetValue(parentObject);

                                if (val != this.value)
                                {
                                    this.value = val;
                                    fInfo.SetValue(parentObject, this.value);
                                    didSet = true;
                                }
                            }
                            else if (mi[0] is PropertyInfo)
                            {
                                var pInfo = mi[0] as PropertyInfo;
                                if (parentModel.Type is System.Collections.IEnumerable)
                                {
                                    var val = pInfo.GetValue(parentObject, new object[] { index });

                                    if (val != this.value)
                                    {
                                        this.value = val;
                                        pInfo.SetValue(parentObject, this.value, new object[] { index });
                                        didSet = true;
                                    }
                                }
                                else
                                {
                                    var val = pInfo.GetValue(parentObject);

                                    if (val != this.value)
                                    {
                                        this.value = val;
                                        pInfo.SetValue(parentObject, this.value);
                                        didSet = true;
                                    }
                                }
                            }
                            else
                            {
                                this.value = value;
                                didSet = true;
                            }

                            if (PropertyChanged != null && didSet)
                                PropertyChanged(this, new PropertyChangedEventArgs("Value"));
                        }
                    }
                    catch
                    {
                        throw new MemberAccessException("The member cannot be set in this way.");
                    }
                }
            }
        }
        private object value;

What can I say, reflection is handy for a debugger.

 

Another contender, though for a different language:

mt.__index = function(self,key)
    local k = self.data[key]
    local metatable = getmetatable(k)
    if key == 'data' or not k then
        return rawget(self, key)
    elseif type(k) ~= 'function' and (metatable == nil or metatable.__call == nil) then
        return k
    else
        return function(...)
            if self == ... then
                return k(self.data, select(2,...))
            else
                return k(...)
            end
        end
    end
end

One does not underestimate the power of metatables in Lua, particularly when having to differentiate between userdata fields and Lua table fields.

code of conduct - report abuse