我想将一个函数从一个类引用到另一个类。
我有第一堂课:
using Jumper.Core.Model;
using Jumper.Core.PlatformServices;
using Jumper.Core.PlatformServices.Storage;
using Jumper.Core.Services;
using System;
using System.Globalization;
using System.Threading.Tasks;
namespace Jumper.Core.Client
{
public sealed class Client
<TBaseSaveData,
TDeviceServiceHelper,
TGeoLocationService,
TNetworkService>
where TBaseSaveData : BaseSaveDataService, new()
where TDeviceServiceHelper : IDeviceServiceHelper, new()
where TGeoLocationService : IGeoLocationService, new()
where TNetworkService : INetworkService, new()
{
private readonly int appId;
private readonly string appVersion;
public readonly ServiceFactory<TBaseSaveData,
TDeviceServiceHelper,
TGeoLocationService,
TNetworkService> serviceFactory;
#region Constructors
internal Client
(int appId, string appVersion)
{
this.appId = appId;
this.appVersion = appVersion;
this.serviceFactory = new ServiceFactory<TBaseSaveData,
TDeviceServiceHelper,
TGeoLocationService,
TNetworkService>();
}
#endregion
public async Task<TrackingItem> CreateDefaultItem()
{
Tuple<double, double> location = await this.serviceFactory.GeoLocationService
.GetUnifiedGeoLocation();
var assemblyService = this.serviceFactory.AssemblyInfoService;
var deviceService = this.serviceFactory.DeviceServiceHelper;
var seconds = (DateTime.UtcNow - this.serviceFactory.TrackingService.StartTime).TotalSeconds;
TrackingItem item = new TrackingItem()
{
//APP
AppId = this.appId,
RunningSeconds = (int)seconds,
AppVersion = this.appVersion,
Language = CultureInfo.CurrentCulture.TwoLetterISOLanguageName,
Country = CultureInfo.CurrentCulture.Name.Substring(3, 2),
};
return item;
}
我想在其他类中使用CreateDefaultItem()函数,但我不能...... 这就是我的所作所为,并不起作用:
using Jumper.Core;
...
...
Jumper.Core.Client.CreateDefaultItem();
我需要做什么才能在另一个类上使用CreateDefaultItem函数?
谢谢!
答案 0 :(得分:0)
而不是:
public async Task<TrackingItem> CreateDefaultItem()
你需要这个:
public static async Task<TrackingItem> CreateDefaultItem()
请注意添加的“static”关键字。这使得方法调用独立于类的任何特定实例。
然而,缺点是方法体只能使用类的其他静态成员(静态字段,静态属性,静态方法)。