【发布时间】:2018-08-13 15:48:27
【问题描述】:
我收到此错误:
CS0034 C# 运算符“-”在“long”和“ulong”类型的操作数上不明确
我尝试了几种方法,但我无法解决它。
这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Text;
namespace IEI.Label
{
public class ReferenceNumber
{
private string macAddress;
private HiResolutionTimer timer;
private ulong counterAtStart;
private ulong epochTime;
private Random random;
private readonly static Lazy<ReferenceNumber> _instance;
public static ReferenceNumber Instance
{
get
{
return ReferenceNumber._instance.Value;
}
}
static ReferenceNumber()
{
ReferenceNumber._instance = new Lazy<ReferenceNumber>(() => new
ReferenceNumber());
}
public ReferenceNumber()
{
this.random = new Random();
this.macAddress = this.GetHashedMacAddress();
this.timer = new HiResolutionTimer();
this.counterAtStart = (ulong)this.timer.Value;
DateTime now = DateTime.Now;
this.epochTime = (ulong)((now.Ticks - 621355968000000000L) / (long)10);
}
private string ConvertBase10ToBase36(ulong number)
{
string str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string str1 = "";
ulong length = (ulong)str.Length;
ulong num = number;
do
{
int num1 = (int)(num % length);
num /= length;
string str2 = str.Substring(num1, 1);
str1 = string.Concat(str2, str1);
}
while (num > (long)0);
return str1;
}
private string GetHashedMacAddress()
{
string machineName = (from nic in
(IEnumerable<NetworkInterface>)NetworkInterface.GetAllNetworkInterfaces()
where nic.OperationalStatus == OperationalStatus.Up
select nic.GetPhysicalAddress().ToString()).FirstOrDefault<string>();
if (string.IsNullOrEmpty(machineName))
{
machineName = Environment.MachineName;
}
return this.Hash(machineName);
}
private string GetRandomBase36DigitsSafe(int numRandomCharacters)
{
byte[] numArray = new byte[8];
this.random.NextBytes(numArray);
string base36 = this.ConvertBase10ToBase36(BitConverter.ToUInt64(numArray, 0));
base36 = (base36.Length <= numRandomCharacters ?
base36.PadLeft(numRandomCharacters, '0') : base36.Substring(0,
numRandomCharacters));
return base36;
}
private string Hash(string input)
{
string base36;
using (SHA1Managed sHA1Managed = new SHA1Managed())
{
byte[] numArray = sHA1Managed.ComputeHash(Encoding.UTF8.GetBytes(input));
base36 = this.ConvertBase10ToBase36(BitConverter.ToUInt64(numArray, 0));
}
return base36;
}
public string NewId(int numTimestampCharacters = 10, int numMacAddressCharacters = 2, int numRandomCharacters = 4)
{
string str = this.macAddress;
str = (str.Length <= numMacAddressCharacters ?
str.PadLeft(numMacAddressCharacters, '0') : str.Substring(0,
numMacAddressCharacters));
string randomBase36DigitsSafe =
this.GetRandomBase36DigitsSafe(numRandomCharacters);
//Here I have got the problem
***ulong value = this.timer.Value - this.counterAtStart * **;
ulong frequency = (ulong)((double)((float)(value * (long)1000000)) /
(double)this.timer.Frequency);
string base36 = this.ConvertBase10ToBase36(this.epochTime + frequency);
base36 = (base36.Length <= numTimestampCharacters ?
base36.PadLeft(numTimestampCharacters, '0') : base36.Substring(base36.Length
- numTimestampCharacters, numTimestampCharacters));
return string.Concat(base36, str, randomBase36DigitsSafe);
}
}
}
【问题讨论】:
-
让它们都变长怎么样?
-
也许读过minimal reproducible example 的想法,重点是“最小”。我认为应该有可能出现相同的错误,例如仅 10 行代码。这使我们为您提供帮助的难度提高了 30 倍。删除与问题无关的所有内容
-
很多时候,编程中的错误信息与一行代码有关,其中提到了行号。这是有用的信息,特别是如果您在 SO 上对受影响的行添加评论。
-
嗨,欢迎来到堆栈溢出。我可以请您阅读有关如何最好地在此论坛 (stackoverflow.com/help/how-to-ask) 上提出好问题的指南,并最大限度地提高将来获得帮助的机会吗?
-
我试过了,但没用。错误信息会像这样出现; “错误 CS1503 参数 1:无法从 'ulong' 转换为 'long' IEI.Label”
标签: c#