powered by simpleCommunicator - 2.0.59     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / ASP.NET [игнор отключен] [закрыт для гостей] / Вызов класса
3 сообщений из 3, страница 1 из 1
Вызов класса
    #33831551
t13
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Здравствуйте.
Не подскажете как можно обратиться к классу программно, т.е. когда его имя содержится в переменной.
...
Рейтинг: 0 / 0
Вызов класса
    #33831630
C...R...a...S...H
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Type.GetType()
А потом можно Reflection'ом создать экземпляр.
Все это должно получиться.
using System;


using System.Reflection;


class TypeObjectFromNameApp {


public static void Main(string[] args) {


Type t = Type.GetType("System.Int32"); Console.WriteLine(t.Name); } }


А дальше примерно наким мокаром:
using System;


using System.Reflection;


using System.10;


class LateBindingApp {


public static void Main()


<


string[] fileNames = Directory.GetFiles


(Environment.CurrentDirectory,


CommProtocol.DLLMask); foreach(string fileName in fileNames) {


Console.WriteLine("Loading DLL '{0}'", fileName);


Assembly a = Assembly.LoadFrom(fileName);


Type[] types = a.GetTypes(); foreach(Type t in types) {


if (t.IsSubclassOf(typeof(CommProtocol)))


{


object о = Activator.Createlnstance(t);


Methodlnfo mi = t.GetMethod("DisplayName");


Console.Write("t"); mi.Invoke(o, null); }


else {


Console.WriteLine("tThis DLL does not have " + "CommProtocol-derived class defined"); } } > > }


----------------------------------------
Knowledge is P...O...w...E...R!
My site
...
Рейтинг: 0 / 0
Вызов класса
    #33834381
t13
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
большое спасибо

Type.GetType()
А потом можно Reflection'ом создать экземпляр.
Все это должно получиться.
using System;


using System.Reflection;


class TypeObjectFromNameApp {


public static void Main(string[] args) {


Type t = Type.GetType("System.Int32"); Console.WriteLine(t.Name); } }


А дальше примерно наким мокаром:
using System;


using System.Reflection;


using System.10;


class LateBindingApp {


public static void Main()


<


string[] fileNames = Directory.GetFiles


(Environment.CurrentDirectory,


CommProtocol.DLLMask); foreach(string fileName in fileNames) {


Console.WriteLine("Loading DLL '{0}'", fileName);


Assembly a = Assembly.LoadFrom(fileName);


Type[] types = a.GetTypes(); foreach(Type t in types) {


if (t.IsSubclassOf(typeof(CommProtocol)))


{


object о = Activator.Createlnstance(t);


Methodlnfo mi = t.GetMethod("DisplayName");


Console.Write("t"); mi.Invoke(o, null); }


else {


Console.WriteLine("tThis DLL does not have " + "CommProtocol-derived class defined"); } } > > }
...
Рейтинг: 0 / 0
3 сообщений из 3, страница 1 из 1
Форумы / ASP.NET [игнор отключен] [закрыт для гостей] / Вызов класса
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]