Здравствуйте, TK, Вы писали:
TK>Можно. Точно так же, как и public методы. Для поиска таких методов надо посмотреть на enum BindingFlags и значение NotPublic
Получить информацию можно, вызвать нельзя.
using System;
using System.Reflection;
namespace ConsoleApplication1
{
/// <summary>
/// Summary description for Class1.
/// </summary>class MyClass
{
public void MyMethod()
{
Console.WriteLine("public");
}
private void MyMethod2()
{
Console.WriteLine("private");
}
}
public class Type_GetMethod
{
public static void Main()
{
try
{
// Get the type of 'MyClass'.
Type myType = typeof(MyClass);
// Get the method information of the type 'MyClass'.
MethodInfo myMethodInfo = myType.GetMethod("MyMethod",BindingFlags.Public|BindingFlags.Instance );
// Displays the member info to the console.
Console.WriteLine("The signature of 'MyMethod' is : {0}", myMethodInfo.ToString());
// Get the method information of the type 'MyClass'.
myMethodInfo = myType.GetMethod("MyMethod2", BindingFlags.NonPublic|BindingFlags.Instance );
// Displays the member info to the console.
Console.WriteLine("The signature of 'MyMethod2' is : {0}", myMethodInfo.ToString());
MyClass c = new MyClass ();
c.GetType().InvokeMember ("MyMethod", BindingFlags.InvokeMethod, null, c, new object [] {});
c.GetType().InvokeMember ("MyMethod2", BindingFlags.NonPublic|BindingFlags.InvokeMethod, null, c, new object [] {});
// ИСКЛЮЧЕНИЕ !!!
}
catch(Exception e)
{
Console.WriteLine("Exception : {0}", e.Message);
}
Console.ReadLine();
}
}
}
Hello, "U-4X-96" > > TK>Можно. Точно так же, как и public методы. Для поиска таких методов надо посмотреть на enum BindingFlags и значение NotPublic > > Получить информацию можно, вызвать нельзя. >
Можно
using System;
using System.Reflection;
namespace ConsoleApplication1
{
/// <summary>
/// Summary description for Class1.
/// </summary>class MyClass
{
public void MyMethod()
{
Console.WriteLine("public");
}
private void MyMethod2()
{
Console.WriteLine("private");
}
}
public class Type_GetMethod
{
public static void Main()
{
try
{
// Get the type of 'MyClass'.
Type myType = typeof(MyClass);
// Get the method information of the type 'MyClass'.
MethodInfo myMethodInfo = myType.GetMethod("MyMethod", BindingFlags.Public | BindingFlags.Instance);
// Displays the member info to the console.
Console.WriteLine("The signature of 'MyMethod' is : {0}", myMethodInfo.ToString());
// Get the method information of the type 'MyClass'.
myMethodInfo = myType.GetMethod("MyMethod2", BindingFlags.NonPublic | BindingFlags.Instance);
// Displays the member info to the console.
Console.WriteLine("The signature of 'MyMethod2' is : {0}", myMethodInfo.ToString());
MyClass c = new MyClass();
c.GetType().InvokeMember("MyMethod", BindingFlags.Instance | BindingFlags.Public | BindingFlags.InvokeMethod, null, c, new object[] { });
c.GetType().InvokeMember("MyMethod2", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, c, new object[] { });
}
catch (Exception e)
{
Console.WriteLine("Exception : {0}", e.Message);
}
Console.ReadLine();
}
}
}
Posted via RSDN NNTP Server 2.0
Если у Вас нет паранойи, то это еще не значит, что они за Вами не следят.
Здравствуйте, Аноним, Вы писали:
А>Можно ли вызывать защищенные и приватные методы(поля свойства) через рефлексию? и если да то как?
Рефлекция для этого и создана была что бы иметь полную информацию о метаданных и управлять ею. Это нужно знать всегда, остальное МСДН.
-----------------------------------------
тут может быть ваша реклама
Здравствуйте, U-4X-96, Вы писали:
U49>Здравствуйте, Аноним, Вы писали:
А>>Можно ли вызывать защищенные и приватные методы(поля свойства) через рефлексию? и если да то как?
U49>"Чесными" средствами нет. U49>Различными трюками возможно, но MS с ними будет бороться, так что лудше оставь идею. U49>Зачем тебе это.Зачем тебе это.
Думаю это глупость. Юнит тесты входящие в состав 2005 студии генярт специальный класс который вызывает закрытые методы. Я не думаю что Микрософт будет бороться сама с собой.