需要有关代码c#的帮助

时间:2011-12-05 13:19:23

标签: c#

我在c#

上课
public class CompositeResource : Control
{
    [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
    public ResourceCollection Resources { get { return _resources; } }
}

public class Resource
{
    [Bindable(true), DefaultValue(""), Editor("System.Web.UI.Design.UrlEditor, System.Design, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor)), Description("Specifies the URL of a resource to reference in the CompositeControl. The URL may be relative, root relative or absolute."), UrlProperty]
    public String Url { get; set; }

    [Bindable(true), DefaultValue(""), Description("Specifies the name of a resource to be used as a reference in the CompositeControl. The ReferenceName is typically used in conjunction with the Sprite control.")]
    public String ReferenceName { get; set; }
}

public class ResourceCollection : List<Resource> 
{ 
}

我只需添加多个资源,如

CompositeResource cr = new CompositeResource();
cr.Type = Xpedite.Resources.ResourceType.Css;
cr.ReferenceName = "hello";
cr.Resources.Add({new Resource().Url="\style\p1.css",new Resource().Url="\style\p2.css" });

但最后一行在我想添加多个资源时给出了错误,每个资源实例都有像url这样的属性。 我认为我错了,但无法捕捉到哪里有问题。

2 个答案:

答案 0 :(得分:7)

我认为您的代码应如下所示:

cr.Resources.Add(new Resource(){ Url= @"\style\p1.css" });
cr.Resources.Add(new Resource(){ Url= @"\style\p2.css" });

您当前的代码不是有效的C#代码。

答案 1 :(得分:6)

您的语法错误

cr.Resources.Add({new Resource().Url="\style\p1.css",new Resource().Url="\style\p2.css" });

应该是

cr.Resources.Add(new Resource() { Url = @"\style\p1.css" });
cr.Resources.Add(new Resource() { Url = @"\style\p2.css" });