diff options
Diffstat (limited to 'csharp/src/Google.Protobuf/Reflection')
8 files changed, 52 insertions, 56 deletions
diff --git a/csharp/src/Google.Protobuf/Reflection/FieldAccessorBase.cs b/csharp/src/Google.Protobuf/Reflection/FieldAccessorBase.cs index 3fccf884..82ce5051 100644 --- a/csharp/src/Google.Protobuf/Reflection/FieldAccessorBase.cs +++ b/csharp/src/Google.Protobuf/Reflection/FieldAccessorBase.cs @@ -41,23 +41,23 @@ namespace Google.Protobuf.Reflection /// </summary> internal abstract class FieldAccessorBase : IFieldAccessor { - private readonly Func<object, object> getValueDelegate; + private readonly Func<IMessage, object> getValueDelegate; private readonly FieldDescriptor descriptor; internal FieldAccessorBase(PropertyInfo property, FieldDescriptor descriptor) { this.descriptor = descriptor; - getValueDelegate = ReflectionUtil.CreateFuncObjectObject(property.GetGetMethod()); + getValueDelegate = ReflectionUtil.CreateFuncIMessageObject(property.GetGetMethod()); } public FieldDescriptor Descriptor { get { return descriptor; } } - public object GetValue(object message) + public object GetValue(IMessage message) { return getValueDelegate(message); } - public abstract void Clear(object message); - public abstract void SetValue(object message, object value); + public abstract void Clear(IMessage message); + public abstract void SetValue(IMessage message, object value); } } diff --git a/csharp/src/Google.Protobuf/Reflection/FileDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/FileDescriptor.cs index 72927702..500e467c 100644 --- a/csharp/src/Google.Protobuf/Reflection/FileDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/FileDescriptor.cs @@ -287,23 +287,23 @@ namespace Google.Protobuf.Reflection DescriptorPool pool = new DescriptorPool(dependencies); FileDescriptor result = new FileDescriptor(descriptorData, proto, dependencies, pool, allowUnknownDependencies, generatedCodeInfo); - // TODO(jonskeet): Reinstate these checks, or get rid of them entirely. They aren't in the Java code, - // and fail for the CustomOptions test right now. (We get "descriptor.proto" vs "google/protobuf/descriptor.proto".) - //if (dependencies.Length != proto.DependencyCount) - //{ - // throw new DescriptorValidationException(result, - // "Dependencies passed to FileDescriptor.BuildFrom() don't match " + - // "those listed in the FileDescriptorProto."); - //} - //for (int i = 0; i < proto.DependencyCount; i++) - //{ - // if (dependencies[i].Name != proto.DependencyList[i]) - // { - // throw new DescriptorValidationException(result, - // "Dependencies passed to FileDescriptor.BuildFrom() don't match " + - // "those listed in the FileDescriptorProto."); - // } - //} + // Validate that the dependencies we've been passed (as FileDescriptors) are actually the ones we + // need. + if (dependencies.Length != proto.Dependency.Count) + { + throw new DescriptorValidationException(result, + "Dependencies passed to FileDescriptor.BuildFrom() don't match " + + "those listed in the FileDescriptorProto."); + } + for (int i = 0; i < proto.Dependency.Count; i++) + { + if (dependencies[i].Name != proto.Dependency[i]) + { + throw new DescriptorValidationException(result, + "Dependencies passed to FileDescriptor.BuildFrom() don't match " + + "those listed in the FileDescriptorProto."); + } + } result.CrossLink(); return result; diff --git a/csharp/src/Google.Protobuf/Reflection/IFieldAccessor.cs b/csharp/src/Google.Protobuf/Reflection/IFieldAccessor.cs index f97d73ee..cfe56fde 100644 --- a/csharp/src/Google.Protobuf/Reflection/IFieldAccessor.cs +++ b/csharp/src/Google.Protobuf/Reflection/IFieldAccessor.cs @@ -45,20 +45,18 @@ namespace Google.Protobuf.Reflection /// </summary> FieldDescriptor Descriptor { get; } - // TODO: Should the argument type for these messages be IReflectedMessage? - /// <summary> /// Clears the field in the specified message. (For repeated fields, /// this clears the list.) /// </summary> - void Clear(object message); + void Clear(IMessage message); /// <summary> /// Fetches the field value. For repeated values, this will be an /// <see cref="IList"/> implementation. For map values, this will be an /// <see cref="IDictionary"/> implementation. /// </summary> - object GetValue(object message); + object GetValue(IMessage message); /// <summary> /// Mutator for single "simple" fields only. @@ -68,6 +66,6 @@ namespace Google.Protobuf.Reflection /// Map fields are mutated by fetching the value and manipulating it as a dictionary. /// </remarks> /// <exception cref="InvalidOperationException">The field is not a "simple" field.</exception> - void SetValue(object message, object value); + void SetValue(IMessage message, object value); } }
\ No newline at end of file diff --git a/csharp/src/Google.Protobuf/Reflection/MapFieldAccessor.cs b/csharp/src/Google.Protobuf/Reflection/MapFieldAccessor.cs index 6df4c5f0..9ed7f8c4 100644 --- a/csharp/src/Google.Protobuf/Reflection/MapFieldAccessor.cs +++ b/csharp/src/Google.Protobuf/Reflection/MapFieldAccessor.cs @@ -45,13 +45,13 @@ namespace Google.Protobuf.Reflection { } - public override void Clear(object message) + public override void Clear(IMessage message) { IDictionary list = (IDictionary) GetValue(message); list.Clear(); } - public override void SetValue(object message, object value) + public override void SetValue(IMessage message, object value) { throw new InvalidOperationException("SetValue is not implemented for map fields"); } diff --git a/csharp/src/Google.Protobuf/Reflection/OneofAccessor.cs b/csharp/src/Google.Protobuf/Reflection/OneofAccessor.cs index ff51291b..8714ab18 100644 --- a/csharp/src/Google.Protobuf/Reflection/OneofAccessor.cs +++ b/csharp/src/Google.Protobuf/Reflection/OneofAccessor.cs @@ -41,8 +41,8 @@ namespace Google.Protobuf.Reflection /// </summary> public sealed class OneofAccessor { - private readonly Func<object, int> caseDelegate; - private readonly Action<object> clearDelegate; + private readonly Func<IMessage, int> caseDelegate; + private readonly Action<IMessage> clearDelegate; private OneofDescriptor descriptor; internal OneofAccessor(PropertyInfo caseProperty, MethodInfo clearMethod, OneofDescriptor descriptor) @@ -52,10 +52,10 @@ namespace Google.Protobuf.Reflection throw new ArgumentException("Cannot read from property"); } this.descriptor = descriptor; - caseDelegate = ReflectionUtil.CreateFuncObjectT<int>(caseProperty.GetGetMethod()); + caseDelegate = ReflectionUtil.CreateFuncIMessageT<int>(caseProperty.GetGetMethod()); this.descriptor = descriptor; - clearDelegate = ReflectionUtil.CreateActionObject(clearMethod); + clearDelegate = ReflectionUtil.CreateActionIMessage(clearMethod); } /// <summary> @@ -69,7 +69,7 @@ namespace Google.Protobuf.Reflection /// <summary> /// Clears the oneof in the specified message. /// </summary> - public void Clear(object message) + public void Clear(IMessage message) { clearDelegate(message); } @@ -77,7 +77,7 @@ namespace Google.Protobuf.Reflection /// <summary> /// Indicates which field in the oneof is set for specified message /// </summary> - public FieldDescriptor GetCaseFieldDescriptor(object message) + public FieldDescriptor GetCaseFieldDescriptor(IMessage message) { int fieldNumber = caseDelegate(message); if (fieldNumber > 0) diff --git a/csharp/src/Google.Protobuf/Reflection/ReflectionUtil.cs b/csharp/src/Google.Protobuf/Reflection/ReflectionUtil.cs index 5b3cbb36..df820ca3 100644 --- a/csharp/src/Google.Protobuf/Reflection/ReflectionUtil.cs +++ b/csharp/src/Google.Protobuf/Reflection/ReflectionUtil.cs @@ -56,52 +56,52 @@ namespace Google.Protobuf.Reflection /// Creates a delegate which will cast the argument to the appropriate method target type, /// call the method on it, then convert the result to object. /// </summary> - internal static Func<object, object> CreateFuncObjectObject(MethodInfo method) + internal static Func<IMessage, object> CreateFuncIMessageObject(MethodInfo method) { - ParameterExpression parameter = Expression.Parameter(typeof(object), "p"); + ParameterExpression parameter = Expression.Parameter(typeof(IMessage), "p"); Expression downcast = Expression.Convert(parameter, method.DeclaringType); Expression call = Expression.Call(downcast, method); Expression upcast = Expression.Convert(call, typeof(object)); - return Expression.Lambda<Func<object, object>>(upcast, parameter).Compile(); + return Expression.Lambda<Func<IMessage, object>>(upcast, parameter).Compile(); } /// <summary> /// Creates a delegate which will cast the argument to the appropriate method target type, /// call the method on it, then convert the result to the specified type. /// </summary> - internal static Func<object, T> CreateFuncObjectT<T>(MethodInfo method) + internal static Func<IMessage, T> CreateFuncIMessageT<T>(MethodInfo method) { - ParameterExpression parameter = Expression.Parameter(typeof(object), "p"); + ParameterExpression parameter = Expression.Parameter(typeof(IMessage), "p"); Expression downcast = Expression.Convert(parameter, method.DeclaringType); Expression call = Expression.Call(downcast, method); Expression upcast = Expression.Convert(call, typeof(T)); - return Expression.Lambda<Func<object, T>>(upcast, parameter).Compile(); + return Expression.Lambda<Func<IMessage, T>>(upcast, parameter).Compile(); } /// <summary> /// Creates a delegate which will execute the given method after casting the first argument to /// the target type of the method, and the second argument to the first parameter type of the method. /// </summary> - internal static Action<object, object> CreateActionObjectObject(MethodInfo method) + internal static Action<IMessage, object> CreateActionIMessageObject(MethodInfo method) { - ParameterExpression targetParameter = Expression.Parameter(typeof(object), "target"); + ParameterExpression targetParameter = Expression.Parameter(typeof(IMessage), "target"); ParameterExpression argParameter = Expression.Parameter(typeof(object), "arg"); Expression castTarget = Expression.Convert(targetParameter, method.DeclaringType); Expression castArgument = Expression.Convert(argParameter, method.GetParameters()[0].ParameterType); Expression call = Expression.Call(castTarget, method, castArgument); - return Expression.Lambda<Action<object, object>>(call, targetParameter, argParameter).Compile(); + return Expression.Lambda<Action<IMessage, object>>(call, targetParameter, argParameter).Compile(); } /// <summary> /// Creates a delegate which will execute the given method after casting the first argument to /// the target type of the method. /// </summary> - internal static Action<object> CreateActionObject(MethodInfo method) + internal static Action<IMessage> CreateActionIMessage(MethodInfo method) { - ParameterExpression targetParameter = Expression.Parameter(typeof(object), "target"); + ParameterExpression targetParameter = Expression.Parameter(typeof(IMessage), "target"); Expression castTarget = Expression.Convert(targetParameter, method.DeclaringType); Expression call = Expression.Call(castTarget, method); - return Expression.Lambda<Action<object>>(call, targetParameter).Compile(); + return Expression.Lambda<Action<IMessage>>(call, targetParameter).Compile(); } } }
\ No newline at end of file diff --git a/csharp/src/Google.Protobuf/Reflection/RepeatedFieldAccessor.cs b/csharp/src/Google.Protobuf/Reflection/RepeatedFieldAccessor.cs index acb3c8d5..bd408470 100644 --- a/csharp/src/Google.Protobuf/Reflection/RepeatedFieldAccessor.cs +++ b/csharp/src/Google.Protobuf/Reflection/RepeatedFieldAccessor.cs @@ -45,13 +45,13 @@ namespace Google.Protobuf.Reflection { } - public override void Clear(object message) + public override void Clear(IMessage message) { IList list = (IList) GetValue(message); list.Clear(); } - public override void SetValue(object message, object value) + public override void SetValue(IMessage message, object value) { throw new InvalidOperationException("SetValue is not implemented for repeated fields"); } diff --git a/csharp/src/Google.Protobuf/Reflection/SingleFieldAccessor.cs b/csharp/src/Google.Protobuf/Reflection/SingleFieldAccessor.cs index 851efc26..de92fbc1 100644 --- a/csharp/src/Google.Protobuf/Reflection/SingleFieldAccessor.cs +++ b/csharp/src/Google.Protobuf/Reflection/SingleFieldAccessor.cs @@ -46,8 +46,8 @@ namespace Google.Protobuf.Reflection // and proto2 vs proto3 for non-message types, as proto3 doesn't support "full" presence detection or default // values. - private readonly Action<object, object> setValueDelegate; - private readonly Action<object> clearDelegate; + private readonly Action<IMessage, object> setValueDelegate; + private readonly Action<IMessage> clearDelegate; internal SingleFieldAccessor(PropertyInfo property, FieldDescriptor descriptor) : base(property, descriptor) { @@ -55,12 +55,10 @@ namespace Google.Protobuf.Reflection { throw new ArgumentException("Not all required properties/methods available"); } - setValueDelegate = ReflectionUtil.CreateActionObjectObject(property.GetSetMethod()); + setValueDelegate = ReflectionUtil.CreateActionIMessageObject(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 @@ -70,12 +68,12 @@ namespace Google.Protobuf.Reflection clearDelegate = message => SetValue(message, defaultValue); } - public override void Clear(object message) + public override void Clear(IMessage message) { clearDelegate(message); } - public override void SetValue(object message, object value) + public override void SetValue(IMessage message, object value) { setValueDelegate(message, value); } |