简体   繁体   English

如何在许多项目中使用扩展方法。

[英]how to use extension methods across many projects .

i am learning extension method, a very handy feature, which can save number of hours of coding, provides reusability. 我正在学习扩展方法,这是一个非常方便的功能,可以节省编码的小时数,提供可重用性。 what i'm doing now a days, daily i'm creating 10 extension methods which useful in day to day scenario. 我现在正在做什么,每天我正在创建10种扩展方法,这些方法在日常场景中很有用。 but i'm not getting how to use these extension methods, everytime we need to add dll and reference it. 但是我没有得到如何使用这些扩展方法,每次我们需要添加dll并引用它。 or is there any smart way where we can use . 还是有任何我们可以使用的聪明方式。

suppose 假设

 public static bool isValidMail(this string str)
        {
            Regex reg = new Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");
            return reg.IsMatch(str);
        }

if i created 100 extension methods like this , then for every project should i need to reference this static class dll. 如果我创建了100个这样的扩展方法,那么对于每个项目我应该引用这个静态类dll。 as i work with multplie projects. 因为我与multplie项目合作。 is there any way that we can put these extension methods in centralized location or some assembly cache, where we can easily add using statement and get access to all static methods. 有没有什么方法可以将这些扩展方法放在集中位置或一些程序集缓存中,我们可以轻松添加using语句并访问所有静态方法。

can we do like this ? 我们可以这样做吗?

  1. whenever we do create new project, can VS automatically add the extensionmethods which we created, so that in evey project we can access it. 每当我们创建新项目时,VS都可以自动添加我们创建的扩展方法,以便在evey项目中我们可以访问它。 rather than adding dll everytime 而不是每次都添加DLL

  2. i want to know how you people do. 我想知道你们是怎么做的。 i hope no one down votes it, just curious abt implementation of extension methods 我希望没有人投票,只是好奇的abt实现扩展方法

It sounds like you should have a single project containing related extension methods - and then yes, you'll need to add a reference to that project from every project which needs it. 听起来你应该有一个包含相关扩展方法的项目 - 然后是的,你需要从每个需要它的项目中添加对该项目的引用。 That's a one-time cost. 这是一次性费用。 You could put it in the GAC, but personally I wouldn't - just treat it as another class library you need to depend on, like any other. 可以将它放在GAC中,但我个人不会 - 只需将其视为您需要依赖的另一个类库,就像其他任何类库一样。

  1. You could change the VS 2010 Project Templates to include a "Framework" DLL automatically. 您可以更改VS 2010项目模板以自动包含“框架”DLL。 visual studio templates 视觉工作室模板

  2. I simply keep a common repository of various common dll's and reference the ones I need, this is in SVN as well, I then have specific Snippets for each Framework I have created to make it easy to insert commonly used code. 我只是保留了各种常见dll的公共存储库并引用了我需要的那些,这也是在SVN中,然后我为每个我创建的框架都有特定的Snippets,以便于插入常用的代码。

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

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