aboutsummaryrefslogtreecommitdiff
path: root/csharp/src/ProtocolBuffers/FieldAccess
diff options
context:
space:
mode:
authorJon Skeet <jonskeet@google.com>2015-07-14 10:24:52 +0100
committerJon Skeet <jonskeet@google.com>2015-07-14 10:24:52 +0100
commit9f37de960fec523c42b47d982f3fdab5b2d4d4d9 (patch)
tree63b35102573a757990bb8af5c4a5fde9fe737572 /csharp/src/ProtocolBuffers/FieldAccess
parent24f8626cc918b5aa58d6d3377d295a95f7049776 (diff)
downloadprotobuf-9f37de960fec523c42b47d982f3fdab5b2d4d4d9.tar.gz
protobuf-9f37de960fec523c42b47d982f3fdab5b2d4d4d9.tar.bz2
protobuf-9f37de960fec523c42b47d982f3fdab5b2d4d4d9.zip
Changing reflection namespace (part 1)
- Move types into Google.Protobuf.Reflection - Change codegen to reflect that in generated types Generated code changes coming in part 2
Diffstat (limited to 'csharp/src/ProtocolBuffers/FieldAccess')
-rw-r--r--csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorBase.cs68
-rw-r--r--csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorTable.cs98
-rw-r--r--csharp/src/ProtocolBuffers/FieldAccess/IFieldAccessor.cs72
-rw-r--r--csharp/src/ProtocolBuffers/FieldAccess/MapFieldAccessor.cs59
-rw-r--r--csharp/src/ProtocolBuffers/FieldAccess/OneofAccessor.cs86
-rw-r--r--csharp/src/ProtocolBuffers/FieldAccess/ReflectionUtil.cs106
-rw-r--r--csharp/src/ProtocolBuffers/FieldAccess/RepeatedFieldAccessor.cs60
-rw-r--r--csharp/src/ProtocolBuffers/FieldAccess/SingleFieldAccessor.cs86
8 files changed, 0 insertions, 635 deletions
diff --git a/csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorBase.cs b/csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorBase.cs
deleted file mode 100644
index 2a3e5b8b..00000000
--- a/csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorBase.cs
+++ /dev/null
@@ -1,68 +0,0 @@
-#region Copyright notice and license
-// Protocol Buffers - Google's data interchange format
-// Copyright 2015 Google Inc. All rights reserved.
-// https://developers.google.com/protocol-buffers/
-//
-// Redistribution and use in source and binary forms, with or without
-// modification, are permitted provided that the following conditions are
-// met:
-//
-// * Redistributions of source code must retain the above copyright
-// notice, this list of conditions and the following disclaimer.
-// * Redistributions in binary form must reproduce the above
-// copyright notice, this list of conditions and the following disclaimer
-// in the documentation and/or other materials provided with the
-// distribution.
-// * Neither the name of Google Inc. nor the names of its
-// contributors may be used to endorse or promote products derived from
-// this software without specific prior written permission.
-//
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-#endregion
-
-using System;
-using System.Reflection;
-using Google.Protobuf.Descriptors;
-
-namespace Google.Protobuf.FieldAccess
-{
- /// <summary>
- /// Base class for field accessors.
- /// </summary>
- internal abstract class FieldAccessorBase : IFieldAccessor
- {
- private readonly Func<object, object> getValueDelegate;
- private readonly FieldDescriptor descriptor;
-
- internal FieldAccessorBase(Type type, string propertyName, FieldDescriptor descriptor)
- {
- PropertyInfo property = type.GetProperty(propertyName);
- if (property == null || !property.CanRead)
- {
- throw new ArgumentException("Not all required properties/methods available");
- }
- this.descriptor = descriptor;
- getValueDelegate = ReflectionUtil.CreateFuncObjectObject(property.GetGetMethod());
- }
-
- public FieldDescriptor Descriptor { get { return descriptor; } }
-
- public object GetValue(object message)
- {
- return getValueDelegate(message);
- }
-
- public abstract void Clear(object message);
- public abstract void SetValue(object message, object value);
- }
-}
diff --git a/csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorTable.cs b/csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorTable.cs
deleted file mode 100644
index 80be93f5..00000000
--- a/csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorTable.cs
+++ /dev/null
@@ -1,98 +0,0 @@
-#region Copyright notice and license
-// Protocol Buffers - Google's data interchange format
-// Copyright 2008 Google Inc. All rights reserved.
-// https://developers.google.com/protocol-buffers/
-//
-// Redistribution and use in source and binary forms, with or without
-// modification, are permitted provided that the following conditions are
-// met:
-//
-// * Redistributions of source code must retain the above copyright
-// notice, this list of conditions and the following disclaimer.
-// * Redistributions in binary form must reproduce the above
-// copyright notice, this list of conditions and the following disclaimer
-// in the documentation and/or other materials provided with the
-// distribution.
-// * Neither the name of Google Inc. nor the names of its
-// contributors may be used to endorse or promote products derived from
-// this software without specific prior written permission.
-//
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-#endregion
-
-using System;
-using System.Collections.ObjectModel;
-using Google.Protobuf.Descriptors;
-
-namespace Google.Protobuf.FieldAccess
-{
- /// <summary>
- /// Provides access to fields in generated messages via reflection.
- /// </summary>
- public sealed class FieldAccessorTable
- {
- private readonly ReadOnlyCollection<IFieldAccessor> accessors;
- private readonly ReadOnlyCollection<OneofAccessor> oneofs;
- private readonly MessageDescriptor descriptor;
-
- /// <summary>
- /// Constructs a FieldAccessorTable for a particular message class.
- /// Only one FieldAccessorTable should be constructed per class.
- /// </summary>
- /// <param name="type">The CLR type for the message.</param>
- /// <param name="descriptor">The type's descriptor</param>
- /// <param name="propertyNames">The Pascal-case names of all the field-based properties in the message.</param>
- public FieldAccessorTable(Type type, MessageDescriptor descriptor, string[] propertyNames, string[] oneofPropertyNames)
- {
- this.descriptor = descriptor;
- var accessorsArray = new IFieldAccessor[descriptor.Fields.Count];
- for (int i = 0; i < accessorsArray.Length; i++)
- {
- var field = descriptor.Fields[i];
- var name = propertyNames[i];
- accessorsArray[i] =
- field.IsMap ? new MapFieldAccessor(type, name, field)
- : field.IsRepeated ? new RepeatedFieldAccessor(type, name, field)
- : (IFieldAccessor) new SingleFieldAccessor(type, name, field);
- }
- accessors = new ReadOnlyCollection<IFieldAccessor>(accessorsArray);
- var oneofsArray = new OneofAccessor[descriptor.Oneofs.Count];
- for (int i = 0; i < oneofsArray.Length; i++)
- {
- var oneof = descriptor.Oneofs[i];
- oneofsArray[i] = new OneofAccessor(type, oneofPropertyNames[i], oneof);
- }
- oneofs = new ReadOnlyCollection<OneofAccessor>(oneofsArray);
- }
-
- // TODO: Validate the name here... should possibly make this type a more "general reflection access" type,
- // bearing in mind the oneof parts to come as well.
- /// <summary>
- /// Returns all of the field accessors for the message type.
- /// </summary>
- public ReadOnlyCollection<IFieldAccessor> Accessors { get { return accessors; } }
-
- public ReadOnlyCollection<OneofAccessor> Oneofs { get { return oneofs; } }
-
- // TODO: Review this, as it's easy to get confused between FieldNumber and Index.
- // Currently only used to get an accessor related to a oneof... maybe just make that simpler?
- public IFieldAccessor this[int fieldNumber]
- {
- get
- {
- FieldDescriptor field = descriptor.FindFieldByNumber(fieldNumber);
- return accessors[field.Index];
- }
- }
- }
-} \ No newline at end of file
diff --git a/csharp/src/ProtocolBuffers/FieldAccess/IFieldAccessor.cs b/csharp/src/ProtocolBuffers/FieldAccess/IFieldAccessor.cs
deleted file mode 100644
index d1727cb4..00000000
--- a/csharp/src/ProtocolBuffers/FieldAccess/IFieldAccessor.cs
+++ /dev/null
@@ -1,72 +0,0 @@
-#region Copyright notice and license
-// Protocol Buffers - Google's data interchange format
-// Copyright 2008 Google Inc. All rights reserved.
-// https://developers.google.com/protocol-buffers/
-//
-// Redistribution and use in source and binary forms, with or without
-// modification, are permitted provided that the following conditions are
-// met:
-//
-// * Redistributions of source code must retain the above copyright
-// notice, this list of conditions and the following disclaimer.
-// * Redistributions in binary form must reproduce the above
-// copyright notice, this list of conditions and the following disclaimer
-// in the documentation and/or other materials provided with the
-// distribution.
-// * Neither the name of Google Inc. nor the names of its
-// contributors may be used to endorse or promote products derived from
-// this software without specific prior written permission.
-//
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-#endregion
-
-using Google.Protobuf.Descriptors;
-
-namespace Google.Protobuf.FieldAccess
-{
- /// <summary>
- /// Allows fields to be reflectively accessed.
- /// </summary>
- public interface IFieldAccessor
- {
- /// <summary>
- /// Returns the descriptor associated with this field.
- /// </summary>
- FieldDescriptor Descriptor { get; }
-
- // TODO: Should the argument type for these messages by IReflectedMessage?
-
- /// <summary>
- /// Clears the field in the specified message. (For repeated fields,
- /// this clears the list.)
- /// </summary>
- void Clear(object 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);
-
- /// <summary>
- /// Mutator for single "simple" fields only.
- /// </summary>
- /// <remarks>
- /// Repeated fields are mutated by fetching the value and manipulating it as a list.
- /// 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, or the message is frozen.</exception>
- void SetValue(object message, object value);
- }
-} \ No newline at end of file
diff --git a/csharp/src/ProtocolBuffers/FieldAccess/MapFieldAccessor.cs b/csharp/src/ProtocolBuffers/FieldAccess/MapFieldAccessor.cs
deleted file mode 100644
index 100dbb37..00000000
--- a/csharp/src/ProtocolBuffers/FieldAccess/MapFieldAccessor.cs
+++ /dev/null
@@ -1,59 +0,0 @@
-#region Copyright notice and license
-// Protocol Buffers - Google's data interchange format
-// Copyright 2015 Google Inc. All rights reserved.
-// https://developers.google.com/protocol-buffers/
-//
-// Redistribution and use in source and binary forms, with or without
-// modification, are permitted provided that the following conditions are
-// met:
-//
-// * Redistributions of source code must retain the above copyright
-// notice, this list of conditions and the following disclaimer.
-// * Redistributions in binary form must reproduce the above
-// copyright notice, this list of conditions and the following disclaimer
-// in the documentation and/or other materials provided with the
-// distribution.
-// * Neither the name of Google Inc. nor the names of its
-// contributors may be used to endorse or promote products derived from
-// this software without specific prior written permission.
-//
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-#endregion
-
-using System;
-using System.Collections;
-using Google.Protobuf.Descriptors;
-
-namespace Google.Protobuf.FieldAccess
-{
- /// <summary>
- /// Accessor for map fields.
- /// </summary>
- internal sealed class MapFieldAccessor : FieldAccessorBase
- {
- internal MapFieldAccessor(Type type, string propertyName, FieldDescriptor descriptor) : base(type, propertyName, descriptor)
- {
- }
-
- public override void Clear(object message)
- {
- IDictionary list = (IDictionary) GetValue(message);
- list.Clear();
- }
-
- public override void SetValue(object message, object value)
- {
- throw new InvalidOperationException("SetValue is not implemented for map fields");
- }
- }
-}
diff --git a/csharp/src/ProtocolBuffers/FieldAccess/OneofAccessor.cs b/csharp/src/ProtocolBuffers/FieldAccess/OneofAccessor.cs
deleted file mode 100644
index 590b6309..00000000
--- a/csharp/src/ProtocolBuffers/FieldAccess/OneofAccessor.cs
+++ /dev/null
@@ -1,86 +0,0 @@
-#region Copyright notice and license
-// Protocol Buffers - Google's data interchange format
-// Copyright 2015 Google Inc. All rights reserved.
-// https://developers.google.com/protocol-buffers/
-//
-// Redistribution and use in source and binary forms, with or without
-// modification, are permitted provided that the following conditions are
-// met:
-//
-// * Redistributions of source code must retain the above copyright
-// notice, this list of conditions and the following disclaimer.
-// * Redistributions in binary form must reproduce the above
-// copyright notice, this list of conditions and the following disclaimer
-// in the documentation and/or other materials provided with the
-// distribution.
-// * Neither the name of Google Inc. nor the names of its
-// contributors may be used to endorse or promote products derived from
-// this software without specific prior written permission.
-//
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-#endregion
-
-using Google.Protobuf.Descriptors;
-using System;
-using System.Reflection;
-
-namespace Google.Protobuf.FieldAccess
-{
- /// <summary>
- /// Reflection access for a oneof, allowing clear and "get case" actions.
- /// </summary>
- public sealed class OneofAccessor
- {
- private readonly Func<object, int> caseDelegate;
- private readonly Action<object> clearDelegate;
- private OneofDescriptor descriptor;
-
- internal OneofAccessor(Type type, string propertyName, OneofDescriptor descriptor)
- {
- PropertyInfo property = type.GetProperty(propertyName + "Case");
- if (property == null || !property.CanRead)
- {
- throw new ArgumentException("Not all required properties/methods available");
- }
- this.descriptor = descriptor;
- caseDelegate = ReflectionUtil.CreateFuncObjectT<int>(property.GetGetMethod());
-
- this.descriptor = descriptor;
- MethodInfo clearMethod = type.GetMethod("Clear" + propertyName);
- clearDelegate = ReflectionUtil.CreateActionObject(clearMethod);
- }
-
- public OneofDescriptor Descriptor { get { return descriptor; } }
-
- /// <summary>
- /// Clears the oneof in the specified message.
- /// </summary>
- public void Clear(object message)
- {
- clearDelegate(message);
- }
-
- /// <summary>
- /// Indicates which field in the oneof is set for specified message
- /// </summary>
- public FieldDescriptor GetCaseFieldDescriptor(object message)
- {
- int fieldNumber = caseDelegate(message);
- if (fieldNumber > 0)
- {
- return descriptor.ContainingType.FindFieldByNumber(fieldNumber);
- }
- return null;
- }
- }
-}
diff --git a/csharp/src/ProtocolBuffers/FieldAccess/ReflectionUtil.cs b/csharp/src/ProtocolBuffers/FieldAccess/ReflectionUtil.cs
deleted file mode 100644
index 08ef6c0c..00000000
--- a/csharp/src/ProtocolBuffers/FieldAccess/ReflectionUtil.cs
+++ /dev/null
@@ -1,106 +0,0 @@
-#region Copyright notice and license
-// Protocol Buffers - Google's data interchange format
-// Copyright 2008 Google Inc. All rights reserved.
-// https://developers.google.com/protocol-buffers/
-//
-// Redistribution and use in source and binary forms, with or without
-// modification, are permitted provided that the following conditions are
-// met:
-//
-// * Redistributions of source code must retain the above copyright
-// notice, this list of conditions and the following disclaimer.
-// * Redistributions in binary form must reproduce the above
-// copyright notice, this list of conditions and the following disclaimer
-// in the documentation and/or other materials provided with the
-// distribution.
-// * Neither the name of Google Inc. nor the names of its
-// contributors may be used to endorse or promote products derived from
-// this software without specific prior written permission.
-//
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-#endregion
-
-using System;
-using System.Linq.Expressions;
-using System.Reflection;
-
-namespace Google.Protobuf.FieldAccess
-{
- /// <summary>
- /// The methods in this class are somewhat evil, and should not be tampered with lightly.
- /// Basically they allow the creation of relatively weakly typed delegates from MethodInfos
- /// which are more strongly typed. They do this by creating an appropriate strongly typed
- /// delegate from the MethodInfo, and then calling that within an anonymous method.
- /// Mind-bending stuff (at least to your humble narrator) but the resulting delegates are
- /// very fast compared with calling Invoke later on.
- /// </summary>
- internal static class ReflectionUtil
- {
- /// <summary>
- /// Empty Type[] used when calling GetProperty to force property instead of indexer fetching.
- /// </summary>
- internal static readonly Type[] EmptyTypes = new Type[0];
-
- /// <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 object.
- /// </summary>
- internal static Func<object, object> CreateFuncObjectObject(MethodInfo method)
- {
- ParameterExpression parameter = Expression.Parameter(typeof(object), "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();
- }
-
- /// <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)
- {
- ParameterExpression parameter = Expression.Parameter(typeof(object), "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();
- }
-
- /// <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)
- {
- ParameterExpression targetParameter = Expression.Parameter(typeof(object), "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();
- }
-
- /// <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)
- {
- ParameterExpression targetParameter = Expression.Parameter(typeof(object), "target");
- Expression castTarget = Expression.Convert(targetParameter, method.DeclaringType);
- Expression call = Expression.Call(castTarget, method);
- return Expression.Lambda<Action<object>>(call, targetParameter).Compile();
- }
- }
-} \ No newline at end of file
diff --git a/csharp/src/ProtocolBuffers/FieldAccess/RepeatedFieldAccessor.cs b/csharp/src/ProtocolBuffers/FieldAccess/RepeatedFieldAccessor.cs
deleted file mode 100644
index 8d7ecbaf..00000000
--- a/csharp/src/ProtocolBuffers/FieldAccess/RepeatedFieldAccessor.cs
+++ /dev/null
@@ -1,60 +0,0 @@
-#region Copyright notice and license
-// Protocol Buffers - Google's data interchange format
-// Copyright 2015 Google Inc. All rights reserved.
-// https://developers.google.com/protocol-buffers/
-//
-// Redistribution and use in source and binary forms, with or without
-// modification, are permitted provided that the following conditions are
-// met:
-//
-// * Redistributions of source code must retain the above copyright
-// notice, this list of conditions and the following disclaimer.
-// * Redistributions in binary form must reproduce the above
-// copyright notice, this list of conditions and the following disclaimer
-// in the documentation and/or other materials provided with the
-// distribution.
-// * Neither the name of Google Inc. nor the names of its
-// contributors may be used to endorse or promote products derived from
-// this software without specific prior written permission.
-//
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-#endregion
-
-using System;
-using System.Collections;
-using Google.Protobuf.Descriptors;
-
-namespace Google.Protobuf.FieldAccess
-{
- /// <summary>
- /// Accessor for repeated fields.
- /// </summary>
- internal sealed class RepeatedFieldAccessor : FieldAccessorBase
- {
- internal RepeatedFieldAccessor(Type type, string propertyName, FieldDescriptor descriptor) : base(type, propertyName, descriptor)
- {
- }
-
- public override void Clear(object message)
- {
- IList list = (IList) GetValue(message);
- list.Clear();
- }
-
- public override void SetValue(object message, object value)
- {
- throw new InvalidOperationException("SetValue is not implemented for repeated fields");
- }
-
- }
-}
diff --git a/csharp/src/ProtocolBuffers/FieldAccess/SingleFieldAccessor.cs b/csharp/src/ProtocolBuffers/FieldAccess/SingleFieldAccessor.cs
deleted file mode 100644
index cdc89e8d..00000000
--- a/csharp/src/ProtocolBuffers/FieldAccess/SingleFieldAccessor.cs
+++ /dev/null
@@ -1,86 +0,0 @@
-#region Copyright notice and license
-// Protocol Buffers - Google's data interchange format
-// Copyright 2015 Google Inc. All rights reserved.
-// https://developers.google.com/protocol-buffers/
-//
-// Redistribution and use in source and binary forms, with or without
-// modification, are permitted provided that the following conditions are
-// met:
-//
-// * Redistributions of source code must retain the above copyright
-// notice, this list of conditions and the following disclaimer.
-// * Redistributions in binary form must reproduce the above
-// copyright notice, this list of conditions and the following disclaimer
-// in the documentation and/or other materials provided with the
-// distribution.
-// * Neither the name of Google Inc. nor the names of its
-// contributors may be used to endorse or promote products derived from
-// this software without specific prior written permission.
-//
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-#endregion
-
-using System;
-using System.Reflection;
-using Google.Protobuf.Descriptors;
-
-namespace Google.Protobuf.FieldAccess
-{
- /// <summary>
- /// Accessor for single fields.
- /// </summary>
- internal sealed class SingleFieldAccessor : FieldAccessorBase
- {
- // All the work here is actually done in the constructor - it creates the appropriate delegates.
- // There are various cases to consider, based on the property type (message, string/bytes, or "genuine" primitive)
- // 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;
-
- internal SingleFieldAccessor(Type type, string propertyName, FieldDescriptor descriptor) : base(type, propertyName, descriptor)
- {
- PropertyInfo property = type.GetProperty(propertyName);
- // We know there *is* such a property, or the base class constructor would have thrown. We should be able to write
- // to it though.
- 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);
- }
-
- public override void Clear(object message)
- {
- clearDelegate(message);
- }
-
- public override void SetValue(object message, object value)
- {
- setValueDelegate(message, value);
- }
- }
-}