Autor Beitrag
timo12
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 28



BeitragVerfasst: Do 04.02.10 07:12 
Hallo,

ich möchte gerne einen Namespace während der Laufzeit nach Benutzersteuerelelemten durchsuchen und diese dann witerverwenden (Auswahlliste und dann an ein Conrtol bind, etc).

Wie durchsuche ich ein Namespace nach Benutzersteuerelemnten?

Danke

Timo
danielf
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1012
Erhaltene Danke: 24

Windows XP
C#, Visual Studio
BeitragVerfasst: Do 04.02.10 07:23 
Hallo,

willst du genau einen bestimmten Namespace oder meinst du damit ein Assembly?

Wenn du die UserControls einer Assembly willst hilft dir die Assembly-Klasse. Dort kannst du auch nach dem Namespace filtern. Denk dran, dass UC immer von UC erben und du sie deshalb über die BasisKlasse bestimmen kannst.

Gruß Daniel
timo12 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 28



BeitragVerfasst: Do 04.02.10 07:28 
Ich weiß es nicht so genau, ich habe im Projektmappenexplorer ein Verzeichnis angelegt (z.B. test)
Wenn ich dort ein Benutzersteuerelement anlegen hat es ja den Namespace Project.test.

In dieses Verzeichnis test möchte ich eine ganze Reihe Benutzersteuerelemente reinlegen und an andere Stelle im Programm brauche ich eine Liste alle Benutzersteuerelemente in diesem Verzeichnis test (also im Namespace Project.test).

Geht das irgendwie?


Timo
danielf
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1012
Erhaltene Danke: 24

Windows XP
C#, Visual Studio
BeitragVerfasst: Do 04.02.10 07:35 
Ja, wie oben beschrieben. Ich hab grad mal für dich meine generische Methode dafür rausgesucht:

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
        public static List<T> CreateInstancesOf<T>(Assembly assembly)
        {
            List<T> instances = new List<T>();

            Type[] types = assembly.GetTypes();

            foreach (Type t in types)
            {
                if (t.IsClass && (t.IsSubclassOf(typeof(T)) || 
                    Array.Exists<Type>(t.GetInterfaces(), delegate(Type interfaceType)
                    { 
                        return interfaceType.Equals(typeof(T));
                    })))
                {
                    instances.Add((T)assembly.CreateInstance(t.FullName));
                }
            }

            return instances;
        }


Müsstest du lediglich um den Namespace abfrage erweitern.

Edit: Da fällt mir auf, das diese Funktion in .NET 3.5 mit LINQ und yield viel schöner wäre ;) Das kannst du ja anpassen und dann hier im Forum zur Verfügung stellen :D
timo12 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 28



BeitragVerfasst: Do 04.02.10 08:03 
Ich bin zu blöd dafür.
Ich versteht das nicht.

Kannst du mir evtl. ein Stück Code posten wo ich aus dem Namspace Project.Windows.test eine Liste bekomme mit allen Benutzersteuerelementeb.

Bitte!


Timo
danielf
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1012
Erhaltene Danke: 24

Windows XP
C#, Visual Studio
BeitragVerfasst: Do 04.02.10 08:54 
Hmm... ungetestet

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
        public static IEnumerable<T> CreateInstancesOf<T>(Assembly assembly, string namespaceName)
        {
            return (from type in assembly.GetTypes()
                        where ((type.IsClass && type.IsSubclassOf(typeof(T))) 
                        || type.GetInterfaces().Contains<Type>(typeof(T)) &&
                        type.Namespace.Equals(namespaceName))
                    select type).Cast<T>();
        }


Bitte testen und Feedback geben.

Edit:
Ach zur Sicherheit .. verwendung wäre dann: IEnumerable<UserControl> userControls = CreateInstancesOf<UserControl>(Assembly.GetAssembly(), "Project.test"); oder so ^^
timo12 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 28



BeitragVerfasst: Do 04.02.10 15:02 
Ich bin echt zu blöd dafür. Ich bekomms nicht hin.

Ich nehme deinen Code und das was du zur ausführung geschrieben hast.
Aber Assembly.GetAssembly() geht nicht er will ein type von irgendwas haben.
Ich habe aber keinen, oder?


Timo
danielf
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1012
Erhaltene Danke: 24

Windows XP
C#, Visual Studio
BeitragVerfasst: Do 04.02.10 15:07 
Stimmt, da war ich ungenau!
ausblenden C#-Quelltext
1:
System.Reflection.Assembly.GetExecutingAssembly()					


Hättest du aber mit einer Suche bei Google selber rausfinden können ....
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4708
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Do 04.02.10 15:40 
Sollte die Funktion nicht eher Typen zurückliefern?
Also eher
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
public static IEnumerable<Type> GetInheritorsOf<T>(Assembly assembly, string namespaceName)
{
    return (from type in assembly.GetTypes()
                 where ((type.IsClass && type.IsSubclassOf(typeof(T))) 
                 || type.GetInterfaces().Contains<Type>(typeof(T)) &&
                        type.Namespace.Equals(namespaceName))
             select type);
}


oder falls doch Instanzen gewünscht und gemeint waren sollte man dann auch Instanzen erzeugen und dann natürlich abstrakte Klassen ausschließen ;)

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
public static IEnumerable<T> CreateInstancesOf<T>(Assembly assembly, string namespaceName)
{
    return (from type in assembly.GetTypes()
                 where ((type.IsClass && type.IsSubclassOf(typeof(T)) && !type.IsAbstract)
                 || type.GetInterfaces().Contains<Type>(typeof(T)) &&
                        type.Namespace.Equals(namespaceName))
             select Activator.CreateInstance(type)).Cast<T>();
}
danielf
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1012
Erhaltene Danke: 24

Windows XP
C#, Visual Studio
BeitragVerfasst: Do 04.02.10 15:51 
Na klar... siehe ursprünglicher Code.

Deshalb meinte ich er sollte drüber schauen, weil ich es einfach nur mal hin geklatscht habe. Aber gut, dass du drauf geschaut hast :D So sieht es besser aus und ich werde es bei mir übernehmen. Vor Allem das IsAbstract hatte ich vergessen - hab die Funktion auch noch nicht alzu oft gebraucht.

Gruß Daniel