Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
P
protobuf
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
protobuf
Commits
9163a6f1
Commit
9163a6f1
authored
May 06, 2019
by
Sydney Acksman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add tests for extension accessor
parent
0c80570a
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
132 additions
and
64 deletions
+132
-64
FieldAccessTest.cs
...rp/src/Google.Protobuf.Test/Reflection/FieldAccessTest.cs
+67
-55
SampleMessages.cs
csharp/src/Google.Protobuf.Test/SampleMessages.cs
+58
-3
FieldDescriptor.cs
csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs
+6
-5
ReflectionUtil.cs
csharp/src/Google.Protobuf/Reflection/ReflectionUtil.cs
+1
-1
No files found.
csharp/src/Google.Protobuf.Test/Reflection/FieldAccessTest.cs
View file @
9163a6f1
...
...
@@ -31,10 +31,13 @@
#endregion
using
Google.Protobuf.TestProtos
;
using
Proto2
=
Google
.
Protobuf
.
TestProtos
.
Proto2
;
using
NUnit.Framework
;
using
System
;
using
System.Collections
;
using
System.Collections.Generic
;
using
System.Collections.Generic
;
using
static
Google
.
Protobuf
.
TestProtos
.
Proto2
.
UnittestExtensions
;
namespace
Google.Protobuf.Reflection
{
...
...
@@ -44,36 +47,36 @@ namespace Google.Protobuf.Reflection
public
void
GetValue
()
{
var
message
=
SampleMessages
.
CreateFullTestAllTypes
();
var
fields
=
TestAllTypes
.
Descriptor
.
Fields
;
Assert
.
AreEqual
(
message
.
SingleBool
,
fields
[
TestAllTypes
.
SingleBoolFieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
SingleBytes
,
fields
[
TestAllTypes
.
SingleBytesFieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
SingleDouble
,
fields
[
TestAllTypes
.
SingleDoubleFieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
SingleFixed32
,
fields
[
TestAllTypes
.
SingleFixed32FieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
SingleFixed64
,
fields
[
TestAllTypes
.
SingleFixed64FieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
SingleFloat
,
fields
[
TestAllTypes
.
SingleFloatFieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
SingleForeignEnum
,
fields
[
TestAllTypes
.
SingleForeignEnumFieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
SingleForeignMessage
,
fields
[
TestAllTypes
.
SingleForeignMessageFieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
SingleImportEnum
,
fields
[
TestAllTypes
.
SingleImportEnumFieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
SingleImportMessage
,
fields
[
TestAllTypes
.
SingleImportMessageFieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
SingleInt32
,
fields
[
TestAllTypes
.
SingleInt32FieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
SingleInt64
,
fields
[
TestAllTypes
.
SingleInt64FieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
SingleNestedEnum
,
fields
[
TestAllTypes
.
SingleNestedEnumFieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
SingleNestedMessage
,
fields
[
TestAllTypes
.
SingleNestedMessageFieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
SinglePublicImportMessage
,
fields
[
TestAllTypes
.
SinglePublicImportMessageFieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
SingleSint32
,
fields
[
TestAllTypes
.
SingleSint32FieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
SingleSint64
,
fields
[
TestAllTypes
.
SingleSint64FieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
SingleString
,
fields
[
TestAllTypes
.
SingleStringFieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
SingleSfixed32
,
fields
[
TestAllTypes
.
SingleSfixed32FieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
SingleSfixed64
,
fields
[
TestAllTypes
.
SingleSfixed64FieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
SingleUint32
,
fields
[
TestAllTypes
.
SingleUint32FieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
SingleUint64
,
fields
[
TestAllTypes
.
SingleUint64FieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
OneofBytes
,
fields
[
TestAllTypes
.
OneofBytesFieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
OneofString
,
fields
[
TestAllTypes
.
OneofStringFieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
OneofNestedMessage
,
fields
[
TestAllTypes
.
OneofNestedMessageFieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
OneofUint32
,
fields
[
TestAllTypes
.
OneofUint32FieldNumber
].
Accessor
.
GetValue
(
message
));
var
fields
=
Test
Protos
.
Test
AllTypes
.
Descriptor
.
Fields
;
Assert
.
AreEqual
(
message
.
SingleBool
,
fields
[
Test
Protos
.
Test
AllTypes
.
SingleBoolFieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
SingleBytes
,
fields
[
Test
Protos
.
Test
AllTypes
.
SingleBytesFieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
SingleDouble
,
fields
[
Test
Protos
.
Test
AllTypes
.
SingleDoubleFieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
SingleFixed32
,
fields
[
Test
Protos
.
Test
AllTypes
.
SingleFixed32FieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
SingleFixed64
,
fields
[
Test
Protos
.
Test
AllTypes
.
SingleFixed64FieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
SingleFloat
,
fields
[
Test
Protos
.
Test
AllTypes
.
SingleFloatFieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
SingleForeignEnum
,
fields
[
Test
Protos
.
Test
AllTypes
.
SingleForeignEnumFieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
SingleForeignMessage
,
fields
[
Test
Protos
.
Test
AllTypes
.
SingleForeignMessageFieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
SingleImportEnum
,
fields
[
Test
Protos
.
Test
AllTypes
.
SingleImportEnumFieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
SingleImportMessage
,
fields
[
Test
Protos
.
Test
AllTypes
.
SingleImportMessageFieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
SingleInt32
,
fields
[
Test
Protos
.
Test
AllTypes
.
SingleInt32FieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
SingleInt64
,
fields
[
Test
Protos
.
Test
AllTypes
.
SingleInt64FieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
SingleNestedEnum
,
fields
[
Test
Protos
.
Test
AllTypes
.
SingleNestedEnumFieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
SingleNestedMessage
,
fields
[
Test
Protos
.
Test
AllTypes
.
SingleNestedMessageFieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
SinglePublicImportMessage
,
fields
[
Test
Protos
.
Test
AllTypes
.
SinglePublicImportMessageFieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
SingleSint32
,
fields
[
Test
Protos
.
Test
AllTypes
.
SingleSint32FieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
SingleSint64
,
fields
[
Test
Protos
.
Test
AllTypes
.
SingleSint64FieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
SingleString
,
fields
[
Test
Protos
.
Test
AllTypes
.
SingleStringFieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
SingleSfixed32
,
fields
[
Test
Protos
.
Test
AllTypes
.
SingleSfixed32FieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
SingleSfixed64
,
fields
[
Test
Protos
.
Test
AllTypes
.
SingleSfixed64FieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
SingleUint32
,
fields
[
Test
Protos
.
Test
AllTypes
.
SingleUint32FieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
SingleUint64
,
fields
[
Test
Protos
.
Test
AllTypes
.
SingleUint64FieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
OneofBytes
,
fields
[
Test
Protos
.
Test
AllTypes
.
OneofBytesFieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
OneofString
,
fields
[
Test
Protos
.
Test
AllTypes
.
OneofStringFieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
OneofNestedMessage
,
fields
[
Test
Protos
.
Test
AllTypes
.
OneofNestedMessageFieldNumber
].
Accessor
.
GetValue
(
message
));
Assert
.
AreEqual
(
message
.
OneofUint32
,
fields
[
Test
Protos
.
Test
AllTypes
.
OneofUint32FieldNumber
].
Accessor
.
GetValue
(
message
));
// Just one example for repeated fields - they're all just returning the list
var
list
=
(
IList
)
fields
[
TestAllTypes
.
RepeatedInt32FieldNumber
].
Accessor
.
GetValue
(
message
);
var
list
=
(
IList
)
fields
[
Test
Protos
.
Test
AllTypes
.
RepeatedInt32FieldNumber
].
Accessor
.
GetValue
(
message
);
Assert
.
AreEqual
(
message
.
RepeatedInt32
,
list
);
Assert
.
AreEqual
(
message
.
RepeatedInt32
[
0
],
list
[
0
]);
// Just in case there was any doubt...
...
...
@@ -85,18 +88,27 @@ namespace Google.Protobuf.Reflection
Assert
.
AreEqual
(
"value1"
,
dictionary
[
"key1"
]);
}
[
Test
]
public
void
GetExtensionValue
()
{
var
message
=
SampleMessages
.
CreateFullTestAllExtensions
();
// test that the reflector works, since the reflector just runs through IExtendableMessage
Assert
.
AreEqual
(
message
.
GetExtension
(
OptionalBoolExtension
),
Proto2
.
TestAllExtensions
.
Descriptor
.
FindFieldByNumber
(
OptionalBoolExtension
.
FieldNumber
).
Accessor
.
GetValue
(
message
));
}
[
Test
]
public
void
Clear
()
{
var
message
=
SampleMessages
.
CreateFullTestAllTypes
();
var
fields
=
TestAllTypes
.
Descriptor
.
Fields
;
fields
[
TestAllTypes
.
SingleBoolFieldNumber
].
Accessor
.
Clear
(
message
);
fields
[
TestAllTypes
.
SingleInt32FieldNumber
].
Accessor
.
Clear
(
message
);
fields
[
TestAllTypes
.
SingleStringFieldNumber
].
Accessor
.
Clear
(
message
);
fields
[
TestAllTypes
.
SingleBytesFieldNumber
].
Accessor
.
Clear
(
message
);
fields
[
TestAllTypes
.
SingleForeignEnumFieldNumber
].
Accessor
.
Clear
(
message
);
fields
[
TestAllTypes
.
SingleForeignMessageFieldNumber
].
Accessor
.
Clear
(
message
);
fields
[
TestAllTypes
.
RepeatedDoubleFieldNumber
].
Accessor
.
Clear
(
message
);
var
fields
=
Test
Protos
.
Test
AllTypes
.
Descriptor
.
Fields
;
fields
[
Test
Protos
.
Test
AllTypes
.
SingleBoolFieldNumber
].
Accessor
.
Clear
(
message
);
fields
[
Test
Protos
.
Test
AllTypes
.
SingleInt32FieldNumber
].
Accessor
.
Clear
(
message
);
fields
[
Test
Protos
.
Test
AllTypes
.
SingleStringFieldNumber
].
Accessor
.
Clear
(
message
);
fields
[
Test
Protos
.
Test
AllTypes
.
SingleBytesFieldNumber
].
Accessor
.
Clear
(
message
);
fields
[
Test
Protos
.
Test
AllTypes
.
SingleForeignEnumFieldNumber
].
Accessor
.
Clear
(
message
);
fields
[
Test
Protos
.
Test
AllTypes
.
SingleForeignMessageFieldNumber
].
Accessor
.
Clear
(
message
);
fields
[
Test
Protos
.
Test
AllTypes
.
RepeatedDoubleFieldNumber
].
Accessor
.
Clear
(
message
);
var
expected
=
new
TestAllTypes
(
SampleMessages
.
CreateFullTestAllTypes
())
{
...
...
@@ -123,14 +135,14 @@ namespace Google.Protobuf.Reflection
{
// Just a sample (primitives, messages, enums, strings, byte strings)
var
message
=
SampleMessages
.
CreateFullTestAllTypes
();
var
fields
=
TestAllTypes
.
Descriptor
.
Fields
;
fields
[
TestAllTypes
.
SingleBoolFieldNumber
].
Accessor
.
SetValue
(
message
,
false
);
fields
[
TestAllTypes
.
SingleInt32FieldNumber
].
Accessor
.
SetValue
(
message
,
500
);
fields
[
TestAllTypes
.
SingleStringFieldNumber
].
Accessor
.
SetValue
(
message
,
"It's a string"
);
fields
[
TestAllTypes
.
SingleBytesFieldNumber
].
Accessor
.
SetValue
(
message
,
ByteString
.
CopyFrom
(
99
,
98
,
97
));
fields
[
TestAllTypes
.
SingleForeignEnumFieldNumber
].
Accessor
.
SetValue
(
message
,
ForeignEnum
.
ForeignFoo
);
fields
[
TestAllTypes
.
SingleForeignMessageFieldNumber
].
Accessor
.
SetValue
(
message
,
new
ForeignMessage
{
C
=
12345
});
fields
[
TestAllTypes
.
SingleDoubleFieldNumber
].
Accessor
.
SetValue
(
message
,
20150701.5
);
var
fields
=
Test
Protos
.
Test
AllTypes
.
Descriptor
.
Fields
;
fields
[
Test
Protos
.
Test
AllTypes
.
SingleBoolFieldNumber
].
Accessor
.
SetValue
(
message
,
false
);
fields
[
Test
Protos
.
Test
AllTypes
.
SingleInt32FieldNumber
].
Accessor
.
SetValue
(
message
,
500
);
fields
[
Test
Protos
.
Test
AllTypes
.
SingleStringFieldNumber
].
Accessor
.
SetValue
(
message
,
"It's a string"
);
fields
[
Test
Protos
.
Test
AllTypes
.
SingleBytesFieldNumber
].
Accessor
.
SetValue
(
message
,
ByteString
.
CopyFrom
(
99
,
98
,
97
));
fields
[
Test
Protos
.
Test
AllTypes
.
SingleForeignEnumFieldNumber
].
Accessor
.
SetValue
(
message
,
ForeignEnum
.
ForeignFoo
);
fields
[
Test
Protos
.
Test
AllTypes
.
SingleForeignMessageFieldNumber
].
Accessor
.
SetValue
(
message
,
new
ForeignMessage
{
C
=
12345
});
fields
[
Test
Protos
.
Test
AllTypes
.
SingleDoubleFieldNumber
].
Accessor
.
SetValue
(
message
,
20150701.5
);
var
expected
=
new
TestAllTypes
(
SampleMessages
.
CreateFullTestAllTypes
())
{
...
...
@@ -151,7 +163,7 @@ namespace Google.Protobuf.Reflection
{
IMessage
message
=
SampleMessages
.
CreateFullTestAllTypes
();
var
fields
=
message
.
Descriptor
.
Fields
;
Assert
.
Throws
<
InvalidCastException
>(()
=>
fields
[
TestAllTypes
.
SingleBoolFieldNumber
].
Accessor
.
SetValue
(
message
,
"This isn't a bool"
));
Assert
.
Throws
<
InvalidCastException
>(()
=>
fields
[
Test
Protos
.
Test
AllTypes
.
SingleBoolFieldNumber
].
Accessor
.
SetValue
(
message
,
"This isn't a bool"
));
}
[
Test
]
...
...
@@ -167,7 +179,7 @@ namespace Google.Protobuf.Reflection
{
IMessage
message
=
SampleMessages
.
CreateFullTestAllTypes
();
var
fields
=
message
.
Descriptor
.
Fields
;
Assert
.
Throws
<
InvalidOperationException
>(()
=>
fields
[
TestAllTypes
.
RepeatedDoubleFieldNumber
].
Accessor
.
SetValue
(
message
,
new
double
[
10
]));
Assert
.
Throws
<
InvalidOperationException
>(()
=>
fields
[
Test
Protos
.
Test
AllTypes
.
RepeatedDoubleFieldNumber
].
Accessor
.
SetValue
(
message
,
new
double
[
10
]));
}
[
Test
]
...
...
@@ -175,42 +187,42 @@ namespace Google.Protobuf.Reflection
{
IMessage
message
=
SampleMessages
.
CreateFullTestAllTypes
();
var
fields
=
message
.
Descriptor
.
Fields
;
Assert
.
Throws
<
InvalidCastException
>(()
=>
fields
[
TestAllTypes
.
SingleBoolFieldNumber
].
Accessor
.
GetValue
(
new
TestMap
()));
Assert
.
Throws
<
InvalidCastException
>(()
=>
fields
[
Test
Protos
.
Test
AllTypes
.
SingleBoolFieldNumber
].
Accessor
.
GetValue
(
new
TestMap
()));
}
[
Test
]
public
void
Oneof
()
{
var
message
=
new
TestAllTypes
();
var
descriptor
=
TestAllTypes
.
Descriptor
;
var
descriptor
=
Test
Protos
.
Test
AllTypes
.
Descriptor
;
Assert
.
AreEqual
(
1
,
descriptor
.
Oneofs
.
Count
);
var
oneof
=
descriptor
.
Oneofs
[
0
];
Assert
.
AreEqual
(
"oneof_field"
,
oneof
.
Name
);
Assert
.
IsNull
(
oneof
.
Accessor
.
GetCaseFieldDescriptor
(
message
));
message
.
OneofString
=
"foo"
;
Assert
.
AreSame
(
descriptor
.
Fields
[
TestAllTypes
.
OneofStringFieldNumber
],
oneof
.
Accessor
.
GetCaseFieldDescriptor
(
message
));
Assert
.
AreSame
(
descriptor
.
Fields
[
Test
Protos
.
Test
AllTypes
.
OneofStringFieldNumber
],
oneof
.
Accessor
.
GetCaseFieldDescriptor
(
message
));
message
.
OneofUint32
=
10
;
Assert
.
AreSame
(
descriptor
.
Fields
[
TestAllTypes
.
OneofUint32FieldNumber
],
oneof
.
Accessor
.
GetCaseFieldDescriptor
(
message
));
Assert
.
AreSame
(
descriptor
.
Fields
[
Test
Protos
.
Test
AllTypes
.
OneofUint32FieldNumber
],
oneof
.
Accessor
.
GetCaseFieldDescriptor
(
message
));
oneof
.
Accessor
.
Clear
(
message
);
Assert
.
AreEqual
(
TestAllTypes
.
OneofFieldOneofCase
.
None
,
message
.
OneofFieldCase
);
Assert
.
AreEqual
(
Test
Protos
.
Test
AllTypes
.
OneofFieldOneofCase
.
None
,
message
.
OneofFieldCase
);
}
[
Test
]
public
void
FieldDescriptor_ByName
()
{
var
descriptor
=
TestAllTypes
.
Descriptor
;
var
descriptor
=
Test
Protos
.
Test
AllTypes
.
Descriptor
;
Assert
.
AreSame
(
descriptor
.
Fields
[
TestAllTypes
.
SingleBoolFieldNumber
],
descriptor
.
Fields
[
Test
Protos
.
Test
AllTypes
.
SingleBoolFieldNumber
],
descriptor
.
Fields
[
"single_bool"
]);
}
[
Test
]
public
void
FieldDescriptor_NotFound
()
{
var
descriptor
=
TestAllTypes
.
Descriptor
;
var
descriptor
=
Test
Protos
.
Test
AllTypes
.
Descriptor
;
Assert
.
Throws
<
KeyNotFoundException
>(()
=>
descriptor
.
Fields
[
999999
].
ToString
());
Assert
.
Throws
<
KeyNotFoundException
>(()
=>
descriptor
.
Fields
[
"not found"
].
ToString
());
}
...
...
csharp/src/Google.Protobuf.Test/SampleMessages.cs
View file @
9163a6f1
...
...
@@ -32,7 +32,9 @@
using
System
;
using
Google.Protobuf.TestProtos
;
using
Proto2
=
Google
.
Protobuf
.
TestProtos
.
Proto2
;
using
Proto2
=
Google
.
Protobuf
.
TestProtos
.
Proto2
;
using
static
Google
.
Protobuf
.
TestProtos
.
Proto2
.
UnittestExtensions
;
namespace
Google.Protobuf
{
...
...
@@ -61,7 +63,7 @@ namespace Google.Protobuf
SingleImportMessage
=
new
ImportMessage
{
D
=
20
},
SingleInt32
=
100
,
SingleInt64
=
3210987654321
,
SingleNestedEnum
=
TestAllTypes
.
Types
.
NestedEnum
.
Foo
,
SingleNestedEnum
=
Test
Protos
.
Test
AllTypes
.
Types
.
NestedEnum
.
Foo
,
SingleNestedMessage
=
new
TestAllTypes
.
Types
.
NestedMessage
{
Bb
=
35
},
SinglePublicImportMessage
=
new
PublicImportMessage
{
E
=
54
},
SingleSfixed32
=
-
123
,
...
...
@@ -83,7 +85,7 @@ namespace Google.Protobuf
RepeatedImportMessage
=
{
new
ImportMessage
{
D
=
20
},
new
ImportMessage
{
D
=
25
}
},
RepeatedInt32
=
{
100
,
200
},
RepeatedInt64
=
{
3210987654321
,
Int64
.
MaxValue
},
RepeatedNestedEnum
=
{
Test
AllTypes
.
Types
.
NestedEnum
.
Foo
,
TestAllTypes
.
Types
.
NestedEnum
.
Neg
},
RepeatedNestedEnum
=
{
Test
Protos
.
TestAllTypes
.
Types
.
NestedEnum
.
Foo
,
TestProtos
.
TestAllTypes
.
Types
.
NestedEnum
.
Neg
},
RepeatedNestedMessage
=
{
new
TestAllTypes
.
Types
.
NestedMessage
{
Bb
=
35
},
new
TestAllTypes
.
Types
.
NestedMessage
{
Bb
=
10
}
},
RepeatedPublicImportMessage
=
{
new
PublicImportMessage
{
E
=
54
},
new
PublicImportMessage
{
E
=
-
1
}
},
RepeatedSfixed32
=
{
-
123
,
123
},
...
...
@@ -149,5 +151,57 @@ namespace Google.Protobuf
OneofString
=
"Oneof string"
};
}
public
static
Proto2
.
TestAllExtensions
CreateFullTestAllExtensions
()
{
var
message
=
new
Proto2
.
TestAllExtensions
();
message
.
SetExtension
(
OptionalBoolExtension
,
true
);
message
.
SetExtension
(
OptionalBytesExtension
,
ByteString
.
CopyFrom
(
1
,
2
,
3
,
4
));
message
.
SetExtension
(
OptionalDoubleExtension
,
23.5
);
message
.
SetExtension
(
OptionalFixed32Extension
,
23u
);
message
.
SetExtension
(
OptionalFixed64Extension
,
1234567890123u
);
message
.
SetExtension
(
OptionalFloatExtension
,
12.25f
);
message
.
SetExtension
(
OptionalForeignEnumExtension
,
Proto2
.
ForeignEnum
.
ForeignBar
);
message
.
SetExtension
(
OptionalForeignMessageExtension
,
new
Proto2
.
ForeignMessage
{
C
=
10
});
message
.
SetExtension
(
OptionalImportEnumExtension
,
Proto2
.
ImportEnum
.
ImportBaz
);
message
.
SetExtension
(
OptionalImportMessageExtension
,
new
Proto2
.
ImportMessage
{
D
=
20
});
message
.
SetExtension
(
OptionalInt32Extension
,
100
);
message
.
SetExtension
(
OptionalInt64Extension
,
3210987654321
);
message
.
SetExtension
(
OptionalNestedEnumExtension
,
Proto2
.
TestAllTypes
.
Types
.
NestedEnum
.
Foo
);
message
.
SetExtension
(
OptionalNestedMessageExtension
,
new
Proto2
.
TestAllTypes
.
Types
.
NestedMessage
{
Bb
=
35
});
message
.
SetExtension
(
OptionalPublicImportMessageExtension
,
new
Proto2
.
PublicImportMessage
{
E
=
54
});
message
.
SetExtension
(
OptionalSfixed32Extension
,
-
123
);
message
.
SetExtension
(
OptionalSfixed64Extension
,
-
12345678901234
);
message
.
SetExtension
(
OptionalSint32Extension
,
-
456
);
message
.
SetExtension
(
OptionalSint64Extension
,
-
12345678901235
);
message
.
SetExtension
(
OptionalStringExtension
,
"test"
);
message
.
SetExtension
(
OptionalUint32Extension
,
UInt32
.
MaxValue
);
message
.
SetExtension
(
OptionalUint64Extension
,
UInt64
.
MaxValue
);
message
.
SetExtension
(
OptionalGroupExtension
,
new
Proto2
.
OptionalGroup_extension
{
A
=
10
});
message
.
GetOrRegisterExtension
(
RepeatedBoolExtension
).
AddRange
(
new
[]
{
true
,
false
});
message
.
GetOrRegisterExtension
(
RepeatedBytesExtension
).
AddRange
(
new
[]
{
ByteString
.
CopyFrom
(
1
,
2
,
3
,
4
),
ByteString
.
CopyFrom
(
5
,
6
),
ByteString
.
CopyFrom
(
new
byte
[
1000
])
});
message
.
GetOrRegisterExtension
(
RepeatedDoubleExtension
).
AddRange
(
new
[]
{
-
12.25
,
23.5
});
message
.
GetOrRegisterExtension
(
RepeatedFixed32Extension
).
AddRange
(
new
[]
{
UInt32
.
MaxValue
,
23u
});
message
.
GetOrRegisterExtension
(
RepeatedFixed64Extension
).
AddRange
(
new
[]
{
UInt64
.
MaxValue
,
1234567890123u
l
});
message
.
GetOrRegisterExtension
(
RepeatedFloatExtension
).
AddRange
(
new
[]
{
100f
,
12.25f
});
message
.
GetOrRegisterExtension
(
RepeatedForeignEnumExtension
).
AddRange
(
new
[]
{
Proto2
.
ForeignEnum
.
ForeignFoo
,
Proto2
.
ForeignEnum
.
ForeignBar
});
message
.
GetOrRegisterExtension
(
RepeatedForeignMessageExtension
).
AddRange
(
new
[]
{
new
Proto2
.
ForeignMessage
(),
new
Proto2
.
ForeignMessage
{
C
=
10
}
});
message
.
GetOrRegisterExtension
(
RepeatedImportEnumExtension
).
AddRange
(
new
[]
{
Proto2
.
ImportEnum
.
ImportBaz
,
Proto2
.
ImportEnum
.
ImportFoo
});
message
.
GetOrRegisterExtension
(
RepeatedImportMessageExtension
).
AddRange
(
new
[]
{
new
Proto2
.
ImportMessage
{
D
=
20
},
new
Proto2
.
ImportMessage
{
D
=
25
}
});
message
.
GetOrRegisterExtension
(
RepeatedInt32Extension
).
AddRange
(
new
[]
{
100
,
200
});
message
.
GetOrRegisterExtension
(
RepeatedInt64Extension
).
AddRange
(
new
[]
{
3210987654321
,
Int64
.
MaxValue
});
message
.
GetOrRegisterExtension
(
RepeatedNestedEnumExtension
).
AddRange
(
new
[]
{
Proto2
.
TestAllTypes
.
Types
.
NestedEnum
.
Foo
,
Proto2
.
TestAllTypes
.
Types
.
NestedEnum
.
Neg
});
message
.
GetOrRegisterExtension
(
RepeatedNestedMessageExtension
).
AddRange
(
new
[]
{
new
Proto2
.
TestAllTypes
.
Types
.
NestedMessage
{
Bb
=
35
},
new
Proto2
.
TestAllTypes
.
Types
.
NestedMessage
{
Bb
=
10
}
});
message
.
GetOrRegisterExtension
(
RepeatedSfixed32Extension
).
AddRange
(
new
[]
{
-
123
,
123
});
message
.
GetOrRegisterExtension
(
RepeatedSfixed64Extension
).
AddRange
(
new
[]
{
-
12345678901234
,
12345678901234
});
message
.
GetOrRegisterExtension
(
RepeatedSint32Extension
).
AddRange
(
new
[]
{
-
456
,
100
});
message
.
GetOrRegisterExtension
(
RepeatedSint64Extension
).
AddRange
(
new
[]
{
-
12345678901235
,
123
});
message
.
GetOrRegisterExtension
(
RepeatedStringExtension
).
AddRange
(
new
[]
{
"foo"
,
"bar"
});
message
.
GetOrRegisterExtension
(
RepeatedUint32Extension
).
AddRange
(
new
[]
{
UInt32
.
MaxValue
,
UInt32
.
MinValue
});
message
.
GetOrRegisterExtension
(
RepeatedUint64Extension
).
AddRange
(
new
[]
{
UInt64
.
MaxValue
,
UInt32
.
MinValue
});
message
.
GetOrRegisterExtension
(
RepeatedGroupExtension
).
AddRange
(
new
[]
{
new
Proto2
.
RepeatedGroup_extension
{
A
=
10
},
new
Proto2
.
RepeatedGroup_extension
{
A
=
20
}
});
message
.
SetExtension
(
OneofStringExtension
,
"Oneof string"
);
return
message
;
}
}
}
\ No newline at end of file
csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs
View file @
9163a6f1
...
...
@@ -380,7 +380,12 @@ namespace Google.Protobuf.Reflection
}
private
IFieldAccessor
CreateAccessor
()
{
{
if
(
Extension
!=
null
)
{
return
new
ExtensionAccessor
(
this
);
}
// If we're given no property name, that's because we really don't want an accessor.
// This could be because it's a map message, or it could be that we're loading a FileDescriptor dynamically.
// TODO: Support dynamic messages.
...
...
@@ -389,10 +394,6 @@ namespace Google.Protobuf.Reflection
return
null
;
}
if
(
Extension
!=
null
)
{
return
new
ExtensionAccessor
(
this
);
}
var
property
=
ContainingType
.
ClrType
.
GetProperty
(
propertyName
);
if
(
property
==
null
)
{
...
...
csharp/src/Google.Protobuf/Reflection/ReflectionUtil.cs
View file @
9163a6f1
...
...
@@ -123,7 +123,7 @@ namespace Google.Protobuf.Reflection
/// the type that declares the method, and the second argument to the first parameter type of the method.
/// </summary>
internal
static
IExtensionReflectionHelper
CreateExtensionHelper
(
Extension
extension
)
=>
(
IExtensionReflectionHelper
)
Activator
.
CreateInstance
(
typeof
(
ExtensionReflectionHelper
<,>).
MakeGenericType
(
extension
.
TargetType
,
extension
.
GetType
().
GenericTypeArguments
[
1
]));
(
IExtensionReflectionHelper
)
Activator
.
CreateInstance
(
typeof
(
ExtensionReflectionHelper
<,>).
MakeGenericType
(
extension
.
TargetType
,
extension
.
GetType
().
GenericTypeArguments
[
1
])
,
extension
);
/// <summary>
/// Creates a reflection helper for the given type arguments. Currently these are created on demand
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment