English 中文(简体)
是否有任何可能的办法,有相同的名称和同一类定义的物体和一系列物体?
原标题:Is there any possible way of having an object and an array of objects with the same name and with the same class definition?
  • 时间:2012-04-08 16:11:22
  •  标签:
  • c#
  • c#-4.0

我正在从动态更新的json中检索数据。 有时含有X类物体,有时还有X类物体。 我需要一个能够在上述任何局势中开展工作的家长级Y。 物体名称:x保持不变。

class Y
{
    public X x { get; set; }
    public X[] x { get; set; }
}
class X
{
    public int a { get; set; }
}

This shows an error: the type Y already contains a definition for x
Is there any possible way to do it??

问题回答

您之所以出现错误,是C#,并不允许有两种名称的变量。

你不能宣布

SomeMethod()
{
    int x = 0;
    string x = "something";
}

如果是你的话,我或许会做如下工作。

 class Y
{
   public List<X> listOfX{get; set;}
}
class X
{
    public int a { get; set; }
}

你可以有一个名单。 如果只有一个要素,则有一个规模1要素清单。

C# spec 指出,一类成员必须有一个独一无二的名称(我假定是防止成员提及模糊不清)。 见

www.un.org/spanish/ecosoc 由于名称空间或类型的每一成员必须有一个独一无二的名称,因此,完全合格的名称或名称始终独特。

如果你需要一个称为X的特性,就可以使用动态关键词,一次性返还一个X类的单一价值,另一个X类。

class Y
{
    public dynamic x { get { return IfMonday() ? new X() : new X[100]; set; }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签