简体   繁体   English

泛型类型的构造函数约束还是只是在我的泛型类型构造函数中检查约束?

[英]Constructor constraint on generic types or simply check for constraint within my generic type constructor?

I have a generic class DirectorySource<T> which depends on an interface IDirectorySearch<T> . 我有一个通用类DirectorySource<T> ,它依赖于接口IDirectorySearch<T>

Both are generics and have the same type constraint: 两者都是泛型,并且具有相同的类型约束:

public class DirectorySource<T> where T : IDirectoryEntry { }

public interface IDirectorySearcher<T> where T : IDirectoryEntry { }

So, for instance, when I want a source to manipulate groups, I would go this way: 因此,例如,当我想要一个源来操纵组时,我将采用这种方式:

IDirectorySearcher<Group> groupSearcher = new GroupSearcher(ROOT, SCOPE);
IDirectorySource<Group> groups = new DirectorySource(groupSearcher);

What I wish to come up with is to force, when my generic type DirectorySource<T> is of DirectorySource<Group> type, that my searcher is a GroupSearcher , and I don't want one to be able to pass in a UserSearcher , for example. GroupSearcher是,当我的通用类型DirectorySource<T>DirectorySource<Group>类型时,我的搜索器是GroupSearcher ,而我不希望有人能够传递UserSearcher ,例如。

I have read the following articles: 我已阅读以下文章:

  1. C#: Generic types that have a constructor? C#:具有构造函数的泛型类型? ; ;
  2. An Introduction to C# Generics ; C#泛型概论 ;
  3. new Constraint (C# Reference) . 新约束(C#参考)

And I don't seem to get how to handle this kind of constraint with my class. 而且我的班级似乎也没有办法处理这种约束。 As for now, I have the following: 现在,我有以下内容:

public class DirectorySource<T> where T : IDirectoryEntry {
    public DirectorySource(IDirectorySearcher<T> searcher) {
        Searcher = searcher;
    }

    public IDirectorySearcher<T> Searcher { get; private set; }
}

And then, I have the following too: 然后,我也有以下内容:

public class GroupSearcher : IDirectorySearcher<Group> {

    public GroupSearcher(DirectoryEntry root, SearchScope scope) {
        NativeSearcher = new DirectorySearcher();
        NativeSearcher.SearchRoot = root;
        NativeSearcher.SearchScope = scope;
    }

    // Implementing interface...
}

I can't just replace the Searcher property type, as this would cause my generic class to become non-generic. 我不能只替换Searcher属性类型,因为这将导致我的通用类变为非通用类。

Any idea or something I didn't understand correctly about this constructor constraint on how I should go with what I want to accomplish? 关于此构造函数约束(我应该如何完成自己的工作)的任何想法或我不正确理解的东西?

EDIT #1 编辑#1

The reason I want to do so is because one could do the following: 我想要这样做的原因是因为可以执行以下操作:

IDirectorySearcher<User> userSearcher = new UserSearcher(ROOT, SCOPE);
IDirectorySource<Group> groups = new DirectorySource<Group>(userSearcher);

This seems incorrect to me... 对我来说这似乎不正确...

1. Am I missing something obvious here? 1.我在这里遗漏了明显的东西吗? =) =)

Thanks in advance! 提前致谢!

What I wish to come up with is to force, when my generic type DirectorySource is of DirectorySource<Group> type, that my searcher is a GroupSearcher. 我想想的是,当我的通用类型DirectorySource为DirectorySource<Group>类型时,强制我的搜索器为GroupSearcher。

Why? 为什么? That's going against the point of encapsulating the searcher functionality into an interface, surely? 当然,这违背了将搜索器功能封装到界面中的观点? You shouldn't care what the implementation is, so long as it can search for the right kinds of entry. 只要它可以搜索正确的条目,就不必关心实现是什么。

I don't think constructor constraints are really relevant here - that would only allow you to create a new instance of T with no arguments... 我认为构造函数约束在这里并没有真正的意义-仅允许您创建不带参数的T的新实例...

EDIT: I can't see how your proposed problem is actually a problem. 编辑:我看不到您提出的问题实际上是一个问题。 Let's look at the code: 让我们看一下代码:

IDirectorySearcher<User> userSearcher = new UserSearcher(ROOT, SCOPE);
IDirectorySource<Group> groups = new DirectorySource<Group>(userSearcher);

So here the T for DirectorySource<T> is Group . 因此,这里DirectorySource<T>TGroup Now the constructor will require you to pass in an IDirectorySearcher<Group> - which userSearcher isn't, as far as I can see. 现在,构造函数将要求您传入IDirectorySearcher<Group> -据我userSearcheruserSearcher不是。 So this code wouldn't compile, which is what you want. 因此,此代码将无法编译,这就是您想要的。 Where's the problem? 哪里出问题了?

Have you considered: 你有没有考虑过:

public class DirectorySource<TValue, TSearcher> 
            where TValue : IDirectoryEntry 
            where TSearcher : IDirectorySearcher<T>, new() 
{ 
    public DirectorySource(TSearcher seacher) 
    { 
        Searcher = seacher
    } 

    public TSearcher Searcher { get; private set; } 
} 

IDirectorySearcher<Group> groupSearcher = new GroupSearcher(ROOT, SCOPE);     
var  groups = new DirectorySource<Group, GroupSearcher>(groupSearcher); 

我认为,要获得所需的唯一方法(据我所知)是如果向IDirectorySearcher添加GetSource方法,该方法返回IDirectorySource<T>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM