我有两节课,BaseClass和Person.将Person类从继承BaseClass.然后我使用以下通用方法.
public static PropertyInfo GetProperty<T>(T item, Func<PropertyInfo, bool> predicate)
{
return GetProperty(typeof(T), predicate);
}
Run Code Online (Sandbox Code Playgroud)
在BaseClass我有一个方法,调用GetProperty
public class BaseClass
{
public void DoSomething()
{
var property = GetProperty(new Person(), (property) => p.Name == "Name");
}
}
Run Code Online (Sandbox Code Playgroud)
然后我从单元测试中调用此方法.
var person = new Person();
person.DoSomething();
Run Code Online (Sandbox Code Playgroud)
何时typeof(T)使用,BaseClass返回.如果我使用item.GetType()那么返回Person.如果我在调试器中检查它们,T是类型BaseClass和item类型Person.为什么它不能推断T是typeof(Person),当它已经知道item是一个人吗?
我上面的例子是错误的,当我打电话给GetProperty时,我通过了this.
public class …Run Code Online (Sandbox Code Playgroud) 我试图使用Dapper将模型保存到数据库.我使用输入/输出参数设置参数,该参数是int用于更新的现有主键值.
public async Task<TKey> SaveAsync<TKey>(IGraph builder, IDataContext context = null)
{
var parameters = this.GetParametersFromDefinition(builder, DefinitionDirection.In);
// See if we have a key defined. If not, we assume this is a new insert.
// Otherwise we pull the key out and assume it's an update.
PropertyDefinition key = builder.GetKey();
if (key != null)
{
parameters.Add(key.ResolvedName, key.PropertyValue, null, ParameterDirection.InputOutput);
}
else
{
throw new InvalidOperationException("The data graph did not have a primary key defined for it.");
}
await this.ExecuteProcedure(parameters, …Run Code Online (Sandbox Code Playgroud) 我有一个名为 SelectedSections 的属性,它是从部分集合中分配的。每个部分都包含一个 BidItems 集合,其中包含 1,000 多个项目。当我选择一个部分时,我需要使用一组已过滤的项目来刷新视图数据绑定到的项目集合。
public Section SelectedSection
{
get
{
return selectedSection;
}
set
{
this.SetPropertyByReference(ref this.selectedSection, value);
if (value != null)
{
this.BidItems = value.BidItems
.Where(item =>
!item.Description.ToLower().Contains("flagger") ||
!item.Description.ToLower().Contains("civilian flagger") ||
!item.Description.ToLower().Contains("law enforcement"))
.ToList();
}
this.MptPayment.EditedItem.DiaryPayItem.Section = value;
}
}
Run Code Online (Sandbox Code Playgroud)
我必须过滤掉大约十几个不同类型的项目(为了清楚起见,我只显示了 3 个)。在我的Where子句中,在检查集合是否包含我要过滤掉的内容之前,我将所有内容都转换为小写。
我意识到这会产生大量垃圾,因为集合中 1,000 多个项目中的每一个都会为小写Description内容创建一个新字符串。我的问题是,对集合中的每个项目执行十几次会比我只检查所有已知的变体更昂贵吗?忽略我可能会错过一个变化的事实,因为我对哪个更快的理论更感兴趣。
上面的列表是所有已知的变体。我想知道哪条路线更贵。迭代集合来检查每个已知条件将足够快,而不会产生这么多垃圾的开销。要么对每个项目/描述枚举一次以上,以便找到全部,要么对每个项目/描述枚举一次,同时在堆上创建垃圾字符串,然后进行 GC。
请注意,当用户执行其工作时,此属性可能会重新设置数十次。因此将执行大量(数万次)字符串比较。
我意识到相对于应用程序的其他部分来说,这是一个便宜的操作;我想了解更多信息以进行自我教育,而不是担心实际应用程序中的性能下降。
我需要提供一个路由机制,其中路由是在运行时从用户帐户创建生成的。例如http://mysite/username/home。
我认为这可以通过路由来完成,但我不确定从哪里开始使用 ASP.Net Core。我在网上看到了一些 MVC 5 的示例,但 ASP.Net Core 处理路由的方式似乎有点不同。如何确保网站不会混淆http://mysite/username/news用户自定义登录页面和http://mysite/news网站新闻页面?
我有一个模型实现两者INotifyPropertyChanged和INotifyDataErrorInfo.当我修改了属性时,属性更改了事件触发,但由于某种原因,当我引发Error事件处理程序时,UI会调用GetErrors方法.这导致验证错误未呈现给UI.
有人可以看看我如何设置INotifyDataErrorInfo并告诉我我是否做错了什么?
基础模型实施
public class BaseChangeNotify : INotifyPropertyChanged, INotifyDataErrorInfo
{
private bool isDirty;
private Dictionary<string, List<string>> errors = new Dictionary<string, List<string>>();
public BaseChangeNotify()
{
}
public event PropertyChangedEventHandler PropertyChanged;
public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
public bool IsDirty
{
get
{
return this.isDirty;
}
set
{
this.isDirty = value;
this.OnPropertyChanged();
}
}
public bool HasErrors
{
get
{
return this.errors.Count(e => e.GetType() == typeof(ErrorMessage)) > 0;
}
}
public IEnumerable GetErrors(string propertyName)
{
if (string.IsNullOrEmpty(propertyName) ||
!this.errors.ContainsKey(propertyName))
{ …Run Code Online (Sandbox Code Playgroud) 我使用 EPPlus 生成了一个 excel 文件,在 MS Office 2007 中一切似乎都很完美,但客户端使用的是 MS Office 2010/2013,并且在第 29 行之后没有设置行高。
这是一个非常奇怪的问题,我已经尝试这样做了 4 天,但无法解决它。
我附上了一个来自 excel 的问题截图。
List<spGetInventoryPrintForLabel> inventories = coreInventory.GetInventoryListForLabel(inventoryIDs);
string fileName = Path.Combine(Server.MapPath("~/ExcelLabelDocuments"), "Skyltar " + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".xlsx");
ExcelPackage pack = new ExcelPackage();
ExcelWorksheet ws = pack.Workbook.Worksheets.Add("Blad1");
ws.PrinterSettings.PaperSize = ePaperSize.A4;
ws.Cells["A:XFD"].Style.Font.Name = "Arial";
ws.Cells["A:XFD"].Style.Font.Size = 10;
ws.DefaultRowHeight = 16.5;
ws.View.ShowGridLines = false;
ws.Column(1).Width = ws.Column(3).Width = ws.Column(4).Width = ws.Column(6).Width = 4;
ws.Column(2).Width = ws.Column(5).Width = 34.5;
int sizeRowOfLabel = 10;
int recordCount = 0; …Run Code Online (Sandbox Code Playgroud) 当我在代码中遇到异常时,它会一直抛出并冒泡回app.gics文件.如果我在try/catch中包装导致异常的方法并不重要,它仍然会回到App实例.
这是我尝试使用的方法:
public static async Task Clear()
{
userSessionToken = string.Empty;
var appdata = ApplicationData.Current;
StorageFile file = await appdata.LocalFolder.GetFileAsync("parseSession");
try
{
await file.DeleteAsync(StorageDeleteOption.PermanentDelete);
}
catch (FileNotFoundException)
{
return;
}
}
Run Code Online (Sandbox Code Playgroud)
每次我点击DeleteAsync方法,并且该文件不存在,我希望抛出并吞下一个异常.相反,我的捕获永远不会受到打击.它一直冒泡到app.gi文件.
public void InitializeComponent()
{
if (_contentLoaded)
return;
_contentLoaded = true;
#if DEBUG && !DISABLE_XAML_GENERATED_BINDING_DEBUG_OUTPUT
DebugSettings.BindingFailed += (sender, args) =>
{
global::System.Diagnostics.Debug.WriteLine(args.Message);
};
#endif
#if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
UnhandledException += (sender, e) =>
{
// --> THIS Catches the exception <--
if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();
};
#endif
}
Run Code Online (Sandbox Code Playgroud)
我应该注意以下几点:
try …Run Code Online (Sandbox Code Playgroud) 我想知道是否有一种简单的方法来实例化一个继承的对象(知道类名).
假设我有
public abstract Monster{
public string name;
public string specie;
public Stats baseStats;
public string imgDir;
}
Run Code Online (Sandbox Code Playgroud)
和
public Specie1 : Monster{
public new string specie = "Specie1";
public new Stats baseStats = {1,2,3,4,5};
public new imgDir = "dir/specie1.png";
public Specie1(string n){
name = n;
}
}
Run Code Online (Sandbox Code Playgroud)
我的目标是从保存的文件加载怪物,只用一个物种测试它,我做以下事情:
Monster[0] = new Specie1("foo");
Run Code Online (Sandbox Code Playgroud)
但是,如果我有很多物种,有没有办法一般地实例化?
例如:
string specie = loadSpecie();
Monster[] = new specie("foo");
Run Code Online (Sandbox Code Playgroud)
我希望有一种方法可以做到这一点,而无需在任何地方创建大量的开关和ifs.
我在考虑创建一个构造函数
Monster(string species,string name){
if(species == "Specie1"){
return new Specie1(name);
}
}
Run Code Online (Sandbox Code Playgroud)
但我认为这将是可怕的编程,并且根本不可扩展.
在干净安装Visual Studio 2015 Enterprise之后,在运行Windows 10的全新Surface Pro 4上,我的XAML语法突出显示缺失.我在其他3台运行Windows和Visual Studio的计算机上都有XAML语法高亮显示; 这台机器由于某种原因没有它.
我尝试在Visual Studio中重置用户数据,我在安装任何扩展之前注意到了这一点.看来我不是唯一一个遇到这个问题的人.为了让我的语法高亮显示返回,需要执行哪些步骤?Intellisense似乎没有问题,只是缺少突出显示.
请注意,我还尝试将环境字体和颜色重置为默认值; 没有解决这个问题.我的C#和Sql语法突出显示工作没有任何问题.
wpf xaml syntax-highlighting visual-studio visual-studio-2015
当我的应用程序启动时,我在我的一些逻辑中根据该逻辑的结果AppDelegate分配MainPage一个页面.
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init ();
// .....
if(authorizationStatus == PhotoLibraryAuthorizationStatus.Authorized)
{
bokehApp.SetStartupView(serviceLocator.GetService<AlbumsPage>());
}
else
{
bokehApp.SetStartupView(serviceLocator.GetService<StartupPage>());
}
}
Run Code Online (Sandbox Code Playgroud)
在我的app.cs中,我从中分配MainPage给定的视图AppDelegate
public class App : Xamarin.Forms.Application
{
public void SetStartupView(ContentPage page)
{
this.MainPage = new NavigationPage(page);
}
}
Run Code Online (Sandbox Code Playgroud)
在这个例子中,我正在StartupPage进入该SetStartupView(Page)方法.当用户做某事时,我导航到AlbumsPage.
this.Navigation.PushAsync(new AlbumPage());
Run Code Online (Sandbox Code Playgroud)
当我这样做时,AlbumPage创建并导航到; 它的Navigation.NavigationStack集合只包含自身,而不是它刚刚导航过的页面.我想要做的是阻止呼叫this.Navigation.PopAsync()导航回到StartupPage目前正在发生的事情.
最初我只是运行一个循环并弹出原始页面,然后删除所有剩余的页面,在这种情况下StartupPage.
// Grab the current page, as it is about …Run Code Online (Sandbox Code Playgroud) 我正在使用 EventSourcing 在 CQRS 上构建 REST 服务,以跨服务分发对我的域的更改。我启动并运行了 REST 服务,有一个用于创建初始模型的 POST 端点,然后是一系列用于更改模型的 PATCH 端点。每个端点都有一个与之关联的命令,客户端将其作为Content-Type参数发送。例如,Content-Type=application/json;domain-command=create-project。我有以下用于在我的任务/项目管理服务上创建项目记录的端点。
project-renamed使用新项目名称将事件插入到事件存储中。project-rescheduled事件插入到事件存储中,并具有新的项目截止日期。project-status-changed事件插入到事件存储中,并具有新的项目状态(活动、计划、存档等)。project-deleted事件插入到事件存储中传统上,在 REST 服务中,您会提供一个 PUT 端点,以便可以替换记录。我不确定它在事件溯源 + CQRS 模式中是如何工作的。我会只使用 POST 和 PATCH 动词吗?
我担心我太细化了,而且每个字段都不需要与之关联的命令。PUT 端点可用于替换片段。不过我担心的是事件存储会不同步,所以我只是坚持使用 PATCH 端点。这种粒度级别是典型的吗?对于一个模型6 个属性我有5 个命令 …
我正在尝试导航到Microsoft Live oauth 身份验证页面,以便我可以授权我的用户并获取应用程序使用的令牌。当我使用以下NSURL字符串时,我能够导航到该站点并授权我的应用程序,检索令牌。
let stringUrl = "https://login.live.com/oauth20_authorize.srf?client_id=\(clientId)&scope=\(scope)&response_type=code"
let url = NSURL(string: stringUrl)!
Run Code Online (Sandbox Code Playgroud)
但是,我想重定向回我的应用程序(使用SFSafariViewController)。为此,我向我的应用程序添加了一个 URL Scheme,并将其作为 URL 中的 redirect_uri 传入。
let stringUrl = "https://login.live.com/oauth20_authorize.srf?client_id=\(clientId)&scope=\(scope)&response_type=code&redirect_uri=Lifestream://onedrive-oauth-callback"
let url = NSURL(string: stringUrl)!
Run Code Online (Sandbox Code Playgroud)

但是,实时登录站点给了我一个错误,说出现问题并且无法继续。在显示 oauth 登录页面之前会发生此错误。它在导航到 URL 后立即发生。
我是否错误地创建了我的 URL 方案,或者不正确地将方案传递给重定向 uri?我很困惑为什么没有redirect_uri它可以正常工作,但是当我提供它时网站无法加载。

有人可以指出我应该如何将我的应用程序的重定向 url 传递到 oauth 重定向的正确方向吗?
更新
Microsoft 似乎不允许您注册作为应用程序 URL 方案的重定向 URL。我不知道如何将这些信息返回到我的应用程序中,除了只是为我可以指向 MSFT 的网站付费,然后它只会为我重定向到应用程序。

c# ×7
excel ×2
generics ×2
wpf ×2
asp.net-core ×1
asp.net-mvc ×1
cqrs ×1
dapper ×1
data-binding ×1
epplus ×1
inheritance ×1
ios ×1
mvvm ×1
navigation ×1
node.js ×1
nsurl ×1
nullable ×1
oauth-2.0 ×1
oop ×1
performance ×1
rest ×1
swift ×1
xaml ×1