如何根据其属性获取类名称

发布于 2024-12-09 03:40:46 字数 44 浏览 4 评论 0原文

是否可以根据其属性名称获取类名称。如果是的话又如何呢?任何人都可以帮助我。

Is it possible to get the class name given the name of its property. If it is then how? Anyone can help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

短暂陪伴 2024-12-16 03:40:46

我认为这不可能直接实现。您必须通过反射搜索所有类,并且必须在每个类中查找特定属性

I don't think it is directly possible. You will have to search all the classes via reflection and you will have to look for that particular property in each class

岁月打碎记忆 2024-12-16 03:40:46

如果您有 PropertyInfo 那么您可以使用 < href="http://msdn.microsoft.com/en-us/library/system.reflection.memberinfo.declaringtype.aspx" rel="nofollow">DeclaringType 属性。如果你只有一些字符串,你就无法从中得到很多东西。您必须首先获取属性,但要获取属性,您首先需要获取声明类,因此您已经知道声明类。

If you have a PropertyInfo then you could use the DeclaringType property. If you only have some string you cannot get much from it. You will have to first get the property but to get the property you first need to get the declaring class, so you already know the declaring class.

旧故 2024-12-16 03:40:46

Reflections are good idea Try to read this article located at MSDN.com http://msdn.microsoft.com/en-us/library/ms173183(VS.80).aspx

一刻暧昧 2024-12-16 03:40:46

以下是 mscorlib.dll 的示例。它查找具有名为“Capacity”属性的所有类。

Assembly asm = Assembly.Load("mscorlib.dll");
foreach (Type type in asm.GetTypes())
{
    foreach (MemberInfo mem in type.GetMembers())
    {
        if ((mem.MemberType == MemberTypes.Property) && (mem.Name == "Capacity"))
            Console.WriteLine(type);
    }
}

或者,使用 LINQ:

var asm = Assembly.Load("mscorlib.dll");
foreach (var type in from type in asm.GetTypes()
                     from mem in type.GetMembers()
                     where (mem.MemberType == MemberTypes.Property) &&
                        (mem.Name == "Capacity")
                     select type)
    Console.WriteLine(type);

输出如下:

System.Text.StringBuilder
System.Collections.CollectionBase
System.Collections.ArrayList
System.Collections.ArrayList+IListWrapper
System.Collections.ArrayList+SyncArrayList
System.Collections.ArrayList+FixedSizeArrayList
System.Collections.ArrayList+ReadOnlyArrayList
System.Collections.ArrayList+Range
System.Collections.SortedList
System.Collections.SortedList+SyncSortedList
System.Collections.Generic.List`1[T]
System.IO.MemoryStream
System.IO.UnmanagedMemoryStream
System.IO.PinnedBufferMemoryStream
System.IO.UnmanagedMemoryAccessor
System.IO.UnmanagedMemoryStreamWrapper

Here is an example for mscorlib.dll. It finds all classes which have property named "Capacity".

Assembly asm = Assembly.Load("mscorlib.dll");
foreach (Type type in asm.GetTypes())
{
    foreach (MemberInfo mem in type.GetMembers())
    {
        if ((mem.MemberType == MemberTypes.Property) && (mem.Name == "Capacity"))
            Console.WriteLine(type);
    }
}

Or, using LINQ:

var asm = Assembly.Load("mscorlib.dll");
foreach (var type in from type in asm.GetTypes()
                     from mem in type.GetMembers()
                     where (mem.MemberType == MemberTypes.Property) &&
                        (mem.Name == "Capacity")
                     select type)
    Console.WriteLine(type);

Here is the output:

System.Text.StringBuilder
System.Collections.CollectionBase
System.Collections.ArrayList
System.Collections.ArrayList+IListWrapper
System.Collections.ArrayList+SyncArrayList
System.Collections.ArrayList+FixedSizeArrayList
System.Collections.ArrayList+ReadOnlyArrayList
System.Collections.ArrayList+Range
System.Collections.SortedList
System.Collections.SortedList+SyncSortedList
System.Collections.Generic.List`1[T]
System.IO.MemoryStream
System.IO.UnmanagedMemoryStream
System.IO.PinnedBufferMemoryStream
System.IO.UnmanagedMemoryAccessor
System.IO.UnmanagedMemoryStreamWrapper
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文