Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2922-DataCompletenessChartPerf/HeuristicLab.ExtLibs/HeuristicLab.ProtobufCS/2.4.1/ProtobufCS/src/ProtocolBuffers/MessageUtil.cs

Last change on this file was 8295, checked in by abeham, 12 years ago

#1897:

  • Removed protocol buffers 0.9.1
  • Added protocol buffers 2.4.1
  • Updated proto processing command
File size: 5.2 KB
Line 
1#region Copyright notice and license
2
3// Protocol Buffers - Google's data interchange format
4// Copyright 2008 Google Inc.  All rights reserved.
5// http://github.com/jskeet/dotnet-protobufs/
6// Original C++/Java/Python code:
7// http://code.google.com/p/protobuf/
8//
9// Redistribution and use in source and binary forms, with or without
10// modification, are permitted provided that the following conditions are
11// met:
12//
13//     * Redistributions of source code must retain the above copyright
14// notice, this list of conditions and the following disclaimer.
15//     * Redistributions in binary form must reproduce the above
16// copyright notice, this list of conditions and the following disclaimer
17// in the documentation and/or other materials provided with the
18// distribution.
19//     * Neither the name of Google Inc. nor the names of its
20// contributors may be used to endorse or promote products derived from
21// this software without specific prior written permission.
22//
23// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
35#endregion
36
37using System;
38using System.Reflection;
39
40namespace Google.ProtocolBuffers
41{
42    /// <summary>
43    /// Utilities for arbitrary messages of an unknown type. This class does not use
44    /// generics precisely because it is designed for dynamically discovered types.
45    /// </summary>
46    public static class MessageUtil
47    {
48        /// <summary>
49        /// Returns the default message for the given type. If an exception is thrown
50        /// (directly from this code), the message will be suitable to be displayed to a user.
51        /// </summary>
52        /// <param name="type"></param>
53        /// <exception cref="ArgumentNullException">The type parameter is null.</exception>
54        /// <exception cref="ArgumentException">The type doesn't implement IMessage, or doesn't
55        /// have a static DefaultMessage property of the same type, or is generic or abstract.</exception>
56        /// <returns></returns>
57        public static IMessage GetDefaultMessage(Type type)
58        {
59            if (type == null)
60            {
61                throw new ArgumentNullException("type", "No type specified.");
62            }
63            if (type.IsAbstract || type.IsGenericTypeDefinition)
64            {
65                throw new ArgumentException("Unable to get a default message for an abstract or generic type (" +
66                                            type.FullName + ")");
67            }
68            if (!typeof (IMessage).IsAssignableFrom(type))
69            {
70                throw new ArgumentException("Unable to get a default message for non-message type (" + type.FullName +
71                                            ")");
72            }
73            PropertyInfo property = type.GetProperty("DefaultInstance",
74                                                     BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
75            if (property == null)
76            {
77                throw new ArgumentException(type.FullName + " doesn't have a static DefaultInstance property");
78            }
79            if (property.PropertyType != type)
80            {
81                throw new ArgumentException(type.FullName + ".DefaultInstance property is of the wrong type");
82            }
83            return (IMessage) property.GetValue(null, null);
84        }
85
86        /// <summary>
87        /// Returns the default message for the type with the given name. This is just
88        /// a convenience wrapper around calling Type.GetType and then GetDefaultMessage.
89        /// If an exception is thrown, the message will be suitable to be displayed to a user.
90        /// </summary>
91        /// <param name="typeName"></param>
92        /// <exception cref="ArgumentNullException">The typeName parameter is null.</exception>
93        /// <exception cref="ArgumentException">The type doesn't implement IMessage, or doesn't
94        /// have a static DefaultMessage property of the same type, or can't be found.</exception>
95        public static IMessage GetDefaultMessage(string typeName)
96        {
97            if (typeName == null)
98            {
99                throw new ArgumentNullException("typeName", "No type name specified.");
100            }
101            Type type = Type.GetType(typeName);
102            if (type == null)
103            {
104                throw new ArgumentException("Unable to load type " + typeName);
105            }
106            return GetDefaultMessage(type);
107        }
108    }
109}
Note: See TracBrowser for help on using the repository browser.