传递一个“var”;进入另一种方法

发布于 2024-08-26 11:23:58 字数 745 浏览 3 评论 0原文

我可能完全错过了这里的要点但是......

我如何将“var”传递到另一个方法中?

(我正在使用 linq 将 XML 加载到可枚举对象列表中)。

我有不同的对象类型(具有不同的字段),但无论使用哪个对象,我的过程的最后一步都是相同的。

XNamespace xmlns = ScehmaName;
var result = from e in XElement.Load(XMLDocumentLocation).Elements(xmlns + ElementName)
             select new Object1
             {
                 Field1 = (string)e.Element(xmlns + "field1"),
                 Field2 = (string)e.Element(xmlns + "field2")
             };

var result2 = Enumerable.Distinct(result);

对于要处理的不同类型的 XML 文件,此代码会有所不同。但然后我想迭代代码来检查各种问题:

foreach (var w in result2)
{
    if (w.CheckIT())
    {
        //do something
    }  
}

我想要的是基类中方法的最后一步,并且我可以将“var”变量从每个子类传递到其中。

I am probably totally missing the point here but....

How can I pass a 'var' into another method?

(I am using linq to load XML into an Enumerable list of objects).

I have differernt object types (with different fields), but the final step of my process is the same regardless of which object is being used.

XNamespace xmlns = ScehmaName;
var result = from e in XElement.Load(XMLDocumentLocation).Elements(xmlns + ElementName)
             select new Object1
             {
                 Field1 = (string)e.Element(xmlns + "field1"),
                 Field2 = (string)e.Element(xmlns + "field2")
             };

var result2 = Enumerable.Distinct(result);

This code will vary for the different type of XML files that will be processed. But I then want to iterate though the code to check for various issues:

foreach (var w in result2)
{
    if (w.CheckIT())
    {
        //do something
    }  
}

What I would love is the final step to be in a method in the base class, and I can pass the 'var' varible into it from each child class.

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

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

发布评论

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

评论(3

-柠檬树下少年和吉他 2024-09-02 11:23:58

w 的类型是 Object1(在上面编写的代码中)。您可以通过将鼠标悬停在 var 关键字上并查看 Visual Studio 向您显示的工具提示来自行找到这一点。

从您的问题中很难看出,但听起来 CheckIt 方法是在 Object1 的基本类型上定义的。如果是的话,你可以编写你的方法来检查它们

protected void CheckAll<T>(IEnumerable<T> result) where T : <base type with CheckIt method>
{
    foreach(var w in result)
    {
        if(w.CheckIt())
        {
            // do something
        }
    }
}

The type of w is Object1 (in the code as written above). You can find this yourself by hovering your mouse over the var keyword and seeing the tooltip that Visual Studio shows you.

It's hard to tell from your question, but it sounds like the CheckIt method is defined on a base type of Object1. If it is you can write your method to check them as

protected void CheckAll<T>(IEnumerable<T> result) where T : <base type with CheckIt method>
{
    foreach(var w in result)
    {
        if(w.CheckIt())
        {
            // do something
        }
    }
}
朮生 2024-09-02 11:23:58

值得注意的是,没有像“var”变量这样的东西。使用 var 关键字声明的任何变量都具有实际类型(由编译器推断)。您只需不必显式编写此类型,但它仍然存在(并且编译器在开始处理代码时将 var 声明替换为实际类型)。这意味着:

var n = 10 
// .. is exactly the same thing as:
int n = 10

在您的示例中:

var result = 
  from e in ... select new Object1 { ... };
// .. is exactly the same thing as:
IEnumerable<Object1> result = 
  from e in ... select new Object1 { ... };

如果您使用匿名类型(例如 new { Name="Test" }),情况会变得更加复杂,因为这些类型没有您可以使用的名称您的代码(因此您被迫使用 var 来处理它们),但由于您的代码不包含匿名类型,所以这对您来说不应该是问题。认识到 var 功能在技术上与匿名类型无关,这一点很有用。

It is worth noting that there is nothing like "var" variable. Any variable declared using the var keyword has an actual type (that is inferred by the compiler). You only don't have to write this type explicitly, but it still there (and the compiler replaces the var declaration with the actual type when it starts processing your code). This means that:

var n = 10 
// .. is exactly the same thing as:
int n = 10

And in your example:

var result = 
  from e in ... select new Object1 { ... };
// .. is exactly the same thing as:
IEnumerable<Object1> result = 
  from e in ... select new Object1 { ... };

The situation becomes more complicated if you use anonymous types (e.g. new { Name="Test" }) because these don't have a name that you could use in your code (so you're forced to work with them using var), but since your code doesn't contain anonymous types, this shouldn't be a problem for you. It is useful to realize that var is feature that's technically not realted to anonymous types in any way.

你又不是我 2024-09-02 11:23:58

您的对象的类型为 Object1。您应该能够创建这样的方法:

private void CheckAll(IEnumerable<Object1> result)
{
    foreach(var w in result)
    {
        if(w.CheckIt())
        {
            // do something
        }
    }
}

并像这样使用它:

CheckAll(result2);

如果您的函数都处理不同的类型并且 CheckIt 不是在公共基类型中建立的,那么这是不可能的(无反射)在 C# 3 中。C# 4 引入了 动态 键入(在本例中具体为“ducktyping”)将允许这样做。

Your objects are typed as Object1. You should be able to create a method like this:

private void CheckAll(IEnumerable<Object1> result)
{
    foreach(var w in result)
    {
        if(w.CheckIt())
        {
            // do something
        }
    }
}

And use it like this:

CheckAll(result2);

If your functions all deal with different types and CheckIt is not established in a common base type, then this isn't possible (without reflection) in C# 3. C# 4 introduces dynamic typing (specifically "duck typing" in this case) that will allow this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文