小编Bsa*_*sa0的帖子

Json.net序列化特定的私有字段

我有以下课程:

public class TriGrid
{
    private List<HexTile> _hexes;
    //other private fields...
    //other public proprerties
}
Run Code Online (Sandbox Code Playgroud)

我的目标是仅序列化_hexes字段,因此我创建了以下ContractResolver:

internal class TriGridContractResolver : DefaultContractResolver
{
    protected override List<MemberInfo> GetSerializableMembers(Type objectType)
    {
        return new List<MemberInfo> { objectType.GetMember("_hexes", BindingFlags.NonPublic | BindingFlags.Instance)[0] };
    }
}
Run Code Online (Sandbox Code Playgroud)

当我想序列化TriGrid的一个实例时,我做了:

var settings = new JsonSerializerSettings()
{
    ContractResolver = new TriGridContractResolver()
};
var json = JsonConvert.SerializeObject(someTriGrid, settings);
string strintJson = json.ToString();
Run Code Online (Sandbox Code Playgroud)

但当我检查的价值strintJson总是"{}".该_hexes具有的元素,它不是空的.如果我序列化一个特定的HexTile它按预期工作.我在这做错了什么?

c# serialization json.net

30
推荐指数
2
解决办法
3万
查看次数

Directory.CreateDirectory并不总是创建该文件夹

我在路径上创建了一个文件夹C:\Users\MYUSER\Desktop\TEST\.

我有以下代码:

private const string DIR = @"C:\Users\MYUSER\Desktop\TEST\tmp";

static void Main(string[] args)
{
    if (Directory.Exists(DIR))
        Directory.Delete(DIR);

    for (int i = 0; i < 100; i++)
    {
        var dinfo = Directory.CreateDirectory(DIR);
        Directory.Delete(DIR);
    }

    Directory.CreateDirectory(DIR);
}
Run Code Online (Sandbox Code Playgroud)

当我执行代码时,大多数时候它运行正常,我可以看到文件夹中有一个文件夹tmp TEST.

我的问题是,在其他一些时候,Directory.CreateDirectory(DIR)根本不创建目录.我甚至检查了DirectoryInfo它的回报和它的Exists属性是falseDirectory.CreateDirectory(DIR),因为该文件夹不存在,将无法正常工作.对这种奇怪的行为有什么解释吗?

.net c# directory io

8
推荐指数
1
解决办法
3538
查看次数

Android LocationListener、onStatusChanged 和 onProviderDisabled 上的 AbstractMethodError

我有一个带有按钮的简单活动,它使用 LocationManager 来尝试获取当前位置:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button buttonGps = findViewById(R.id.buttonGps);

    final Context activity = this;

    buttonGps.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!checkForPhonePermission()) {
                return;
            }

            LocationManager locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);

            locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, new LocationListener() {
                @Override
                public void onLocationChanged(@NonNull Location location) {
                    Log.d(TAG, location.getLatitude() + "");
                }
            }, Looper.myLooper());
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

我在 Android Studio 中创建了一个 API 级别为 22 的模拟器,在授予权限后,在模拟器的 GPS 开启的情况下,应用程序在单击按钮时崩溃并出现此错误:

java.lang.AbstractMethodError: abstract method "void android.location.LocationListener.onStatusChanged(java.lang.String, int, android.os.Bundle)"
Run Code Online (Sandbox Code Playgroud)

如果我在 GPS 关闭的情况下按下按钮,则会出现此错误:

java.lang.AbstractMethodError: …
Run Code Online (Sandbox Code Playgroud)

android locationmanager

7
推荐指数
3
解决办法
5795
查看次数

TypeScript 通过 ref 参数传递

在 C# 中,可以通过引用传递参数。例如:

    private void Add(ref Node node)
    {
        if (node == null)
        {
            node = new Node();
        }
    }
    Add(ref this.Root);
Run Code Online (Sandbox Code Playgroud)

this.Root执行后不会为空Add(ref this.Root)

从我从 TypeScript 中看到的情况来看,不可能通过引用传递参数。这段代码:

    private addAux(node: Node): void {
        if (node === undefined) {
            node = new Node();
        }
    }
    addAux(this._root);
Run Code Online (Sandbox Code Playgroud)

完成后addAux(this._root)this._root仍将是未定义的,因为它的副本将被传递到addAux.

是否有解决方法可以使refTypeScript 中的 C# 关键字具有相同的功能?

c# typescript

5
推荐指数
1
解决办法
2万
查看次数