예제 #1
0
        internal SingleFieldAccessor(PropertyInfo property, FieldDescriptor descriptor) : base(property, descriptor)
        {
            if (!property.CanWrite)
            {
                throw new ArgumentException("Not all required properties/methods available");
            }
            setValueDelegate = ReflectionUtil.CreateActionObjectObject(property.GetSetMethod());

            var clrType = property.PropertyType;

            // TODO: What should clear on a oneof member do? Clear the oneof?

            // TODO: Validate that this is a reasonable single field? (Should be a value type, a message type, or string/ByteString.)
            object defaultValue =
                typeof(IMessage).IsAssignableFrom(clrType) ? null
                : clrType == typeof(string) ? ""
                : clrType == typeof(ByteString) ? ByteString.Empty
                : Activator.CreateInstance(clrType);

            clearDelegate = message => SetValue(message, defaultValue);
        }