Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DataBinder.cs « System.Web.UI « System.Web « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 37bb71adf517ed34c484f953d27d71735d8b94ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//
// System.Web.UI.DataBinder.cs
//
// Authors:
// 	Duncan Mak  (duncan@ximian.com)
// 	Gonzalo Paniagua Javier (gonzalo@ximian.com)
//
// (C) 2002 Ximian, Inc. (http://www.ximian.com)
//

using System;
using System.Reflection;

namespace System.Web.UI {

	public sealed class DataBinder
	{
		public DataBinder ()
		{
		}

		public static object Eval (object container, string expression)
		{
			return GetPropertyValue (container, expression);
		}

		public static string Eval (object container, string expression, string format)
		{
			return GetPropertyValue (container, expression, format);
		}

		[MonoTODO]
		public static object GetIndexedPropertyValue (object container, string expr)
		{
			throw new NotImplementedException ();
		}

		[MonoTODO]
		public static string GetIndexedPropertyValue (object container, string expr, string format)
		{
			throw new NotImplementedException ();
		}

		public static object GetPropertyValue (object container, string propName)
		{
			if (container == null || propName == null)
				throw new ArgumentException ();

			Type type = container.GetType ();
			PropertyInfo prop = type.GetProperty (propName);
			if (prop == null)
				throw new HttpException ("Property " + propName + " not found in " +
							 type.ToString ());
			MethodInfo getm = prop.GetGetMethod ();
			if (getm == null)
				throw new HttpException ("Cannot find get accessor for " + propName +
							 " in " + type.ToString ());
			
			return getm.Invoke (container, null);
		}

		public static string GetPropertyValue (object container, string propName, string format)
		{
			object result;

			result = GetPropertyValue (container, propName);
			if (result == null)
				return String.Empty;

			if (format == null)
				return result.ToString ();

			return String.Format (format, result);
		}		
	}
}