[C#] GTK#으로 타입의 데이터를 확인하는 프로그램을 작성했는데 제대로 동작하지 않습니다.
글쓴이: HDNua / 작성시간: 목, 2013/01/10 - 11:56오전
MonoDevelop 툴, Gtk# 2.0을 사용하고 있습니다.
프로젝트 작성할 때 Empty Project로 선택하고 Gtk# Support 체크한 후
Reference에 atk-sharp, gdk-sharp, glib-sharp, gtk-sharp를 추가하였습니다.
ILogger.cs
using System;
namespace ReflectionTest
{
public interface ILogger
{
void PrintInterfaces(Type type);
void PrintFields(Type type);
void PrintProperties(Type type);
void PrintEvents(Type type);
void PrintMethods(Type type);
}
}ConsoleLogger.cs
using System;
using System.Reflection;
namespace ReflectionTest
{
public class ConsoleLogger: ILogger
{
// constructor
public ConsoleLogger ()
{
}
// interface: ILogger
void ILogger.PrintInterfaces (Type type)
{
Console.WriteLine ("--- Interfaces ---");
Type[] interfaces = type.GetInterfaces ();
foreach (Type i in interfaces)
Console.WriteLine ("Name: {0}", i.Name);
Console.WriteLine ();
}
void ILogger.PrintFields (Type type)
{
Console.WriteLine ("--- Fields ---");
FieldInfo[] fields = type.GetFields ();
foreach (FieldInfo f in fields)
{
string access = "protected";
if (f.IsPublic) access = "public";
else if (f.IsPrivate) access = "private";
Console.Write ("{0} ", access);
Console.Write ("{0}", f.FieldType.Name);
Console.WriteLine ("{0} ", f.Name);
}
Console.WriteLine ();
}
void ILogger.PrintProperties (Type type)
{
Console.WriteLine ("--- Properties ---");
PropertyInfo[] props = type.GetProperties ();
foreach (PropertyInfo p in props)
{
Console.WriteLine ("{0} {1}",
p.PropertyType.Name, p.Name);
}
Console.WriteLine();
}
void ILogger.PrintEvents (Type type)
{
Console.WriteLine ("--- Events ---");
EventInfo[] events = type.GetEvents ();
foreach (EventInfo e in events)
{
Console.WriteLine ("{0}", e.Name);
}
Console.WriteLine ();
}
void ILogger.PrintMethods (Type type)
{
Console.WriteLine ("--- Methods ---");
MethodInfo[] methods = type.GetMethods ();
foreach (MethodInfo m in methods)
{
Console.Write("{0} {1} (",
m.ReturnType, m.Name);
ParameterInfo[] parameters =
m.GetParameters();
for (int i=0; i<parameters.Length; ++i)
{
Console.Write ("{0}", parameters[i]);
if (i<parameters.Length-1)
Console.Write (", ");
}
Console.WriteLine (")");
}
Console.WriteLine ();
}
}
}StringLogger.cs
using System;
using System.Reflection;
namespace ReflectionTest
{
public class StringLogger: ILogger
{
string log;
public string Log{ get { return log; } }
// constructor
public StringLogger (Type type)
{
log = "";
PrintInterfaces(type);
PrintFields(type);
PrintProperties(type);
PrintEvents(type);
PrintMethods(type);
}
// methods
public void Add (string str)
{
log += str;
}
public void Add (string str, params object[] args)
{
log += string.Format (str, args);
}
public void AddLine ()
{
log += '\n';
}
public void AddLine (string str)
{
Add (str);
AddLine ();
}
public void AddLine (string str, params object[] args)
{
Add (str, args);
AddLine ();
}
// interface: ILogger
public void PrintInterfaces (Type type)
{
AddLine ("--- Interfaces ---");
Type[] interfaces = type.GetInterfaces ();
foreach (Type i in interfaces)
AddLine ("Name: {0}", i.Name);
AddLine ();
}
public void PrintFields (Type type)
{
AddLine ("--- Fields ---");
FieldInfo[] fields = type.GetFields ();
foreach (FieldInfo f in fields)
{
string access = "protected";
if (f.IsPublic) access = "public";
else if (f.IsPrivate) access = "private";
Add ("{0} ", access);
Add ("{0}", f.FieldType.Name);
AddLine ("{0} ", f.Name);
}
AddLine ();
}
public void PrintProperties (Type type)
{
AddLine ("--- Properties ---");
PropertyInfo[] props = type.GetProperties ();
foreach (PropertyInfo p in props)
{
AddLine ("{0} {1}",
p.PropertyType.Name, p.Name);
}
AddLine();
}
public void PrintEvents (Type type)
{
AddLine ("--- Events ---");
EventInfo[] events = type.GetEvents ();
foreach (EventInfo e in events)
{
AddLine ("{0}", e.Name);
}
AddLine ();
}
public void PrintMethods (Type type)
{
AddLine ("--- Methods ---");
MethodInfo[] methods = type.GetMethods ();
foreach (MethodInfo m in methods)
{
Add("{0} {1} (",
m.ReturnType, m.Name);
ParameterInfo[] parameters =
m.GetParameters();
for (int i=0; i<parameters.Length; ++i)
{
Add ("{0}", parameters[i]);
if (i<parameters.Length-1)
Add (", ");
}
AddLine (")");
}
AddLine ();
}
}
}SharpApp.cs
using System;
using System.Reflection;
namespace PrintWidgetEvent
{
public class SharpApp: Gtk.Window
{
SharpApp self;
Gtk.Entry entry;
Gtk.Label label;
Gtk.Button button;
public SharpApp(): base("Widget's event")
{
self = this;
SetDefaultSize (250, 200);
SetPosition(Gtk.WindowPosition.Center);
DeleteEvent += (o, args) => Gtk.Application.Quit ();
// create vertical box
Gtk.VBox vbox = new Gtk.VBox(false, 2);
// create vertical alignment
// and put it into vbox
Gtk.Alignment valign
= new Gtk.Alignment(0, 0, 1, 0);
vbox.Add (valign);
// create another box
// that can pack two or more widgets
Gtk.VBox vbox2 = new Gtk.VBox(false, 2);
valign.Add (vbox2);
// create entry
// and put it into vbox2
entry = new Gtk.Entry();
vbox2.Add (entry);
// create button
// and put it into vbox2
button = new Gtk.Button("Press!");
button.Pressed += OnButtonPressed;
vbox2.Add (button);
// create label
// and put it into vbox2
label = new Gtk.Label("Test");
label.SetAlignment(0, 0);
label.CanFocus = true;
vbox2.Add (label);
// add vbox to window
Add (vbox);
// test
Console.WriteLine ("Type: {0}", this.button.GetType ());
// show all widgets in window
ShowAll ();
}
// event handler
public void OnButtonPressed (object o, EventArgs args)
{
if (entry.Text == "") {
label.Text = "Entry is null";
return;
}
string typestr = entry.Text;
System.Type type = System.Type.GetType (typestr);
if (type == null) {
label.Text = "Type does not exist";
return;
}
ReflectionTest.StringLogger slgr
= new ReflectionTest.StringLogger(type);
label.Text = "Printed";
// label.Text = slgr.Log;
Console.WriteLine ("{0}", entry.Text);
Console.WriteLine (slgr.Log);
// self.SetPosition (Gtk.WindowPosition.Center);
}
// main
public static void Main(string[] args)
{
Gtk.Application.Init ();
new SharpApp();
Gtk.Application.Run ();
}
}
}System, 제가 만든 네임스페이스에 대해서는 제대로 동작하는 것 같은데
유독 Gtk 네임스페이스에 대해서만 제대로 동작을 안 하고 있네요.
도움 부탁드립니다.
Forums:


댓글 달기