我正在尝试创建一个类的实例,我可以通用方式添加到列表中.
我知道type这需要量身定做的,我已经能够使一个类的object使用下面的代码之类的,但我还没有找到一种方法来创建一个施法,让我这个添加到列表中..任何想法?
T与objType相同
public static List<T> LoadList(string fileName, Type objType)
{
List<T> objList = new List<T>();
object o = Activator.CreateInstance(objType);
objList.Add((**o.GetType()**)o);
return objList;
}
Run Code Online (Sandbox Code Playgroud)
如果这是一个更好的方式这样做我也开放的想法:)
我在我的C#应用程序中遇到了问题...我在数据库中有一些学校课程,例如8-B,9-A,10-C,11-C等......当我使用order by子句时为了对它们进行排序,字符串比较将结果表示为
10-C
11-C
8-B
9-A
Run Code Online (Sandbox Code Playgroud)
但我希望在字符串中存在的第一个整数的基础上进行整数排序...即
8-B
9-A
10-C
11-C
Run Code Online (Sandbox Code Playgroud)
希望你能理解......我已经尝试过了,但它会抛出异常
var query = cx.Classes.Select(x=>x.Name)
.OrderBy( x=> new string(x.TakeWhile(char.IsDigit).ToArray()));
Run Code Online (Sandbox Code Playgroud)
请帮帮我...想要在课程的基础上订购....
我研究过的每个地方都应该工作,但它不会...我基本上试图让我的变量商店在一个类中工作在我的主表单上.我一直收到错误代码
无法隐式转换
string为Form1.Employee
我的表格代码
Employee FirstName = new Employee();
if (Directions.Text == "Please Enter Employee First Name")
{
FirstName = Info.Text;
Directions.Text = "Please Enter Employee Last Name";
}
Run Code Online (Sandbox Code Playgroud)
在我的第二堂课中
public class Employee
{
public string FirstName;
}
Run Code Online (Sandbox Code Playgroud) 接口 - I1(包含M1方法)类 - C1:I1(c1实现I1)类 - C2:C1(C2继承C1)
interface I1
{
void M1();
}
class C1 : I1
{
void M1(){}
}
class C2 : C1
{
}
Run Code Online (Sandbox Code Playgroud)
要求: "C1"中不应该有"M1"的实现,基本上"M1"需要在"C2"中实现
如何使用任何OOP概念实现这一目标?
假设我有一个数组
string[] A = {"1","2","3","4","5"}
Run Code Online (Sandbox Code Playgroud)
我希望数组的大小为10,并想在某个索引之后插入空白字符串。
例如,我可以将其设置为10,并在索引3之后插入字符串,这将导致
A = {"1","2","3","4","","","","","","5"}
Run Code Online (Sandbox Code Playgroud)
基本上,给定索引之后的元素将被推到末尾,空白字符串将占据它们之间的空白。
这是我尝试过的方法,但是它只添加了一个字符串,并没有为数组精确设置大小
var foos = new List<string>(A);
foos.Insert(33, "");
foos[32] = "";
A = foos.ToArray();
Run Code Online (Sandbox Code Playgroud) 我正在使用Django Rest Framework,我不知道为什么我会收到此错误.
models.py
from __future__ import unicode_literals
from django.db import models
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
followers = models.ManyToManyField('self', related_name='followees', symmetrical=False)
class Post(models.Model):
author = models.ForeignKey(User, related_name = 'posts')
title = models.CharField(max_length = 255)
body = models.TextField(blank = True, null = True)
class Photo(models.Model):
post = models.ForeignKey(Post, related_name = 'photos')
image = models.ImageField(upload_to = '%Y/%m/%d')
Run Code Online (Sandbox Code Playgroud)
serializers.py
from rest_framework import serializers
from .models import *
class UserSerializer(serializers.ModelSerializer):
# Getting the list of posts made by particular users using the …Run Code Online (Sandbox Code Playgroud) 我收到此错误,但我做了完全相同的事情:
/courses/模块'django.http.request'处的AttributeError没有属性'META'
错误发生在:
from django.shortcuts import render
from django.http import request
from django.http import HttpResponse
from .models import Course
# Create your views here.
def course_list(response):
courses = Course.objects.all()
return render(request, 'courses/course_list.html',{'courses':courses})
# output=', '.join([str(course) for course in courses])
# return HttpResponse(output)
Run Code Online (Sandbox Code Playgroud)
但服务器根本没有显示任何问题。
Performing system checks...
System check identified no issues (0 silenced).
September 13, 2016 - 13:51:18
Django version 1.10.1, using settings 'learning_site.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Run Code Online (Sandbox Code Playgroud) 我将列表与Tag和NavigableString Type混合在一起。我只想访问标签数据。我该如何检查?我想避免以下错误。
AttributeError: type object 'BeautifulSoup' has no attribute 'NavigableString'
Run Code Online (Sandbox Code Playgroud) 我不知道为什么我收到这个错误..
['用户'对象不可迭代]
在这里,我想在主页中打印(登录用户)粉丝.除了错误是我的models.py是对的?追随者和追随机制.
模型
class Follow(models.Model):
following = models.ForeignKey('auth.User', related_name='following')
followers = models.ForeignKey('auth.User', related_name='followers')
Run Code Online (Sandbox Code Playgroud)
视图
def profile(request):
current_user = request.user
twi = Follow.objects.get(pk=current_user.id)
display = twi.followers
return render(request,'home.html' ,
{'detail':display,'user':current_user,})
Run Code Online (Sandbox Code Playgroud)
模板
{% for o in detail %}
<h1>o.followers</h1>
{% endfor %}
Run Code Online (Sandbox Code Playgroud) 我决定使用一个ListBox用于我的应用程序,在这种情况下,慈善命名lb.我继续填充内容(只有少数几个string)并操纵它的大小和外观,然后将它打成我的形状.
在我玩的选项中,我提到了lb.SelectionMode = SelectionMode.MultiSimple;,因为我希望用户能够选择我string的一个或多个.
现在,直到我添加该SelectionMode行的那一点,lb.SelectedIndex总是指向用户选择的选择(显然是在它之后).
但是当选择多个项目时似乎没有这样做.它似乎只是指向选择的"第一" string.
有什么方法我可以告诉它去NEXT选择的项目,或者可能获得一个新的列表,其中包含所有选定项目的索引?
我怎样才能最好地获取用户选择的所有选定字符串?