// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
using System.Text;
namespace Debugger.MetaData
{
public class DebugPropertyInfo : System.Reflection.PropertyInfo, IDebugMemberInfo, IOverloadable
{
DebugType declaringType;
string name;
MethodInfo getMethod;
MethodInfo setMethod;
internal DebugPropertyInfo(DebugType declaringType, string name, MethodInfo getMethod, MethodInfo setMethod)
{
if (getMethod == null && setMethod == null) throw new ArgumentNullException("Both getter and setter can not be null.");
this.declaringType = declaringType;
this.name = name;
this.getMethod = getMethod;
this.setMethod = setMethod;
}
///
public override Type DeclaringType {
get { return declaringType; }
}
/// The AppDomain in which this member is declared
public AppDomain AppDomain {
get { return declaringType.AppDomain; }
}
/// The Process in which this member is declared
public Process Process {
get { return declaringType.Process; }
}
/// The Module in which this member is declared
public Debugger.Module DebugModule {
get { return declaringType.DebugModule; }
}
///
public override int MetadataToken {
get { return 0; }
}
///
public override System.Reflection.Module Module {
get { throw new NotSupportedException(); }
}
///
public override string Name {
get { return name; }
}
/// Name including the declaring type, return type and parameters
public string FullName {
get {
StringBuilder sb = new StringBuilder();
if (this.IsStatic) {
sb.Append("static ");
}
sb.Append(this.PropertyType.Name);
sb.Append(" ");
sb.Append(this.DeclaringType.FullName);
sb.Append(".");
sb.Append(this.Name);
if (GetIndexParameters().Length > 0) {
sb.Append("[");
bool first = true;
foreach(DebugParameterInfo p in GetIndexParameters()) {
if (!first)
sb.Append(", ");
first = false;
sb.Append(p.ParameterType.Name);
sb.Append(" ");
sb.Append(p.Name);
}
sb.Append("]");
}
return sb.ToString();
}
}
///
public override Type ReflectedType {
get { throw new NotSupportedException(); }
}
///
public override object[] GetCustomAttributes(bool inherit)
{
throw new NotSupportedException();
}
///
public override object[] GetCustomAttributes(Type attributeType, bool inherit)
{
throw new NotSupportedException();
}
///
public override bool IsDefined(Type attributeType, bool inherit)
{
return DebugType.IsDefined(this, inherit, attributeType);
}
///
public override PropertyAttributes Attributes {
get { throw new NotSupportedException(); }
}
///
public override bool CanRead {
get { return getMethod != null; }
}
///
public override bool CanWrite {
get { return setMethod != null; }
}
///
public override Type PropertyType {
get {
if (getMethod != null) {
return getMethod.ReturnType;
} else {
return setMethod.GetParameters()[setMethod.GetParameters().Length - 1].ParameterType;
}
}
}
///
public override MethodInfo[] GetAccessors(bool nonPublic)
{
throw new NotSupportedException();
}
// public virtual object GetConstantValue();
///
public override MethodInfo GetGetMethod(bool nonPublic)
{
return getMethod;
}
///
public override ParameterInfo[] GetIndexParameters()
{
if (GetGetMethod() != null) {
return GetGetMethod().GetParameters();
}
if (GetSetMethod() != null) {
List pars = new List();
pars.AddRange(GetSetMethod().GetParameters());
pars.RemoveAt(pars.Count - 1);
return pars.ToArray();
}
return null;
}
// public virtual Type[] GetOptionalCustomModifiers();
// public virtual object GetRawConstantValue();
// public virtual Type[] GetRequiredCustomModifiers();
///
public override MethodInfo GetSetMethod(bool nonPublic)
{
return setMethod;
}
///
public override object GetValue(object obj, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
{
List args = new List();
foreach(object arg in index) {
args.Add((Value)arg);
}
return Value.GetPropertyValue((Value)obj, this, args.ToArray());
}
///
public override void SetValue(object obj, object value, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
{
List args = new List();
foreach(object arg in index) {
args.Add((Value)arg);
}
Value.SetPropertyValue((Value)obj, this, args.ToArray(), (Value)value);
}
public bool IsPublic {
get { return (getMethod ?? setMethod).IsPublic; }
}
public bool IsAssembly {
get { return (getMethod ?? setMethod).IsAssembly; }
}
public bool IsFamily {
get { return (getMethod ?? setMethod).IsFamily; }
}
public bool IsPrivate {
get { return (getMethod ?? setMethod).IsPrivate; }
}
public bool IsStatic {
get { return (getMethod ?? setMethod).IsStatic; }
}
DebugType IDebugMemberInfo.MemberType {
get { return (DebugType)this.PropertyType; }
}
ParameterInfo[] IOverloadable.GetParameters()
{
return GetIndexParameters();
}
IntPtr IOverloadable.GetSignarture()
{
return ((IOverloadable)(getMethod ?? setMethod)).GetSignarture();
}
///
public override string ToString()
{
return this.FullName;
}
}
}