Skip to content

Commit 989af68

Browse files
committed
Add Helpers.PopulateObjects to create a list of Things with a WebAgent
1 parent 944a6f0 commit 989af68

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

RedditSharp/Helpers/Helpers.PopulateObject.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using Newtonsoft.Json;
22
using Newtonsoft.Json.Linq;
33
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using RedditSharp.Interfaces;
47

58
namespace RedditSharp
69
{
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Newtonsoft.Json.Linq;
5+
using RedditSharp.Interfaces;
6+
7+
namespace RedditSharp
8+
{
9+
public partial class Helpers
10+
{
11+
internal static List<T> PopulateObjects<T>(JToken json, IWebAgent webAgent)
12+
where T : ISettableWebAgent, new()
13+
{
14+
if (json.Type != JTokenType.Array)
15+
throw new ArgumentException("must be of type array", nameof(json));
16+
17+
var objects = new List<T>();
18+
19+
for (var i = 0; i < json.Count(); i++)
20+
{
21+
var item = new T();
22+
PopulateObject(json[i], item);
23+
item.WebAgent = webAgent;
24+
objects.Add(item);
25+
}
26+
27+
return objects;
28+
}
29+
}
30+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using Newtonsoft.Json;
2+
3+
namespace RedditSharp.Interfaces
4+
{
5+
internal interface ISettableWebAgent
6+
{
7+
[JsonIgnore]
8+
IWebAgent WebAgent { set; }
9+
}
10+
}

0 commit comments

Comments
 (0)